marcboeker / go-duckdb

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

Static linking on Windows broken #51

Closed marcboeker closed 4 weeks ago

marcboeker commented 2 years 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 10 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 4 months ago

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

localhosted commented 1 month 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

taniabogatsch commented 4 weeks ago

I just merged https://github.com/marcboeker/go-duckdb/pull/248, which bundles a pre-compiled static library for Windows (using MingW). It will be part of the next go-duckdb release sometime next week.

ZenLiuCN commented 4 weeks ago

mingw compiled static libs may not supports all official extensions. suggest that should be noted in readme.

taniabogatsch commented 4 weeks ago

Fair point, I already added a small note to the README, but I'll extend it a bit. We're also looking into adding MingW distributions for our official extensions. We already build them for our Rtools, so we might just have to generalize those.

ZenLiuCN commented 4 weeks ago

Very good news for me. My last trying got blocked by mysql🤔