go-duckdb
go-duckdb copied to clipboard
Static linking on Windows broken
When compiling the amalgamation file to build a static library the compiler complains about definition is marked dllimport. Others have this problem too.
But when adding a -DDUCKDB_BUILD_LIBRARY the errors and warnings are gone and the resulting libduckdb.a is successfully created. But linking agains it throws the following errors (for more examples see log.txt):
C:\Users\dev\AppData\Local\Temp\go-link-2035893457\000007.o: in function `_cgo_37088fb8ef74_Cfunc_duckdb_bind_uint16':
/tmp/go-build/cgo-gcc-prolog:313: undefined reference to `__imp_duckdb_bind_uint16'
This is very weird as the Darwin and Linux builds succeed. The code is located under https://github.com/marcboeker/go-duckdb/tree/windows-builds
Hi @marcboeker! Can I suggest trying with https://github.com/ebitengine/purego instead of cgo?
I also found the following issue that might be of use: https://github.com/golang/go/issues/51007#issuecomment-1369090750
Could you try reproducing with Go 1.20+?
I'm sorry, I only had access to a amd64 Windows VM for a short period of time to test this. ATM I'm not able to setup a amd64 based Windows VM on my M1 Mac, so I can't reproduce this. Do you have access to a Windows machine to test this with 1.20+?
No, unfortunately. I was looking into the issue for https://github.com/cloudquery/cloudquery/issues/8854, but the builds take a couple of hours to complete with the results we already have (+ I believe, the code needs rebasing, as the DuckDB version in the branch is stale).
However, looking at the issue it seems that the thing to look at is this:
- Check that the produced file is able to be linked statically
- Verify that the static linking is used instead of the dynamic one (dynamic may be somehow enforced for Go based on the CGO being used, as the
__imp_prefixes indicate an attempt to link against DLL instead of static linking).
Hi, I just try to using static link under windows.
- compile a static lib from duckdb source (mingw)
- link it with go-duckdb
- run into the same trouble
- found inside duckdb.h
//
// duckdb.h
//
//
//===----------------------------------------------------------------------===//
#pragma once
// duplicate of duckdb/main/winapi.hpp
#ifndef DUCKDB_API
#ifdef _WIN32
#if defined(DUCKDB_BUILD_LIBRARY) && !defined(DUCKDB_BUILD_LOADABLE_EXTENSION)
#define DUCKDB_API __declspec(dllexport)
#else
#define DUCKDB_API //__declspec(dllimport) // !! here
#endif
- comment out that part makes no link error when compile.
Maybe this is related? https://github.com/duckdb/duckdb/issues/12571
@ZenLiuCN Do you have more information on createing the static duckdb build and linking it to go?
I haven't found any instructions on a static build in github.com/duckdb/duckdb/blob/main/Makefile