go-duckdb icon indicating copy to clipboard operation
go-duckdb copied to clipboard

Static linking on Windows broken

Open marcboeker opened this issue 3 years ago • 7 comments

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

marcboeker avatar Oct 15 '22 16:10 marcboeker

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

candiduslynx avatar Apr 13 '23 15:04 candiduslynx

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

candiduslynx avatar Aug 02 '23 11:08 candiduslynx

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

marcboeker avatar Aug 03 '23 07:08 marcboeker

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).

candiduslynx avatar Aug 03 '23 08:08 candiduslynx

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
  1. comment out that part makes no link error when compile.

ZenLiuCN avatar Dec 17 '23 07:12 ZenLiuCN

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

ayuhito avatar Jul 07 '24 15:07 ayuhito

@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

localhosted avatar Sep 12 '24 12:09 localhosted