marcboeker / go-duckdb

go-duckdb provides a database/sql driver for the DuckDB database engine.
MIT License
677 stars 103 forks source link

Static linking on Windows broken #51

Open marcboeker opened 1 year ago

marcboeker commented 1 year ago

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

candiduslynx commented 1 year ago

Hi @marcboeker! Can I suggest trying with https://github.com/ebitengine/purego instead of cgo?

candiduslynx commented 1 year ago

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+?

marcboeker commented 1 year ago

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+?

candiduslynx commented 1 year ago

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:

  1. Check that the produced file is able to be linked statically
  2. 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).
ZenLiuCN commented 9 months ago

Hi, I just try to using static link under windows.

  1. compile a static lib from duckdb source (mingw)
  2. link it with go-duckdb
  3. run into the same trouble
  4. 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


5. comment out that part makes no link error when compile.
ayuhito commented 2 months ago

Maybe this is related? https://github.com/duckdb/duckdb/issues/12571

localhosted commented 2 weeks ago

@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