-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Link error when calling Windows API functions from function in C static library #2073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Quick note: it should actually be Here's what's going on: When you call an extern Windows function from Zig code, Zig keeps track of it and creates that .def file that you pointed out. This will enumerate all the functions that are called, and the corresponding .lib file that is created will be sufficient. However there is no way for Zig to find out about what external function calls from C code should be added to .def files. So there are two workarounds: Link libc. Or, you can enumerate all the external DLL calls that the C code uses, in your Zig source, like this: extern "kernel32" stdcallcc fn LoadLibraryA() void;
comptime {
_ = LoadLibraryA;
} The function prototype doesn't even have to be correct, just the "kernel32" and the name. The A better long term solution will probably come from #514 when we solve the libc on Windows. Probably Zig will simply ship with a complete .def file for kernel32.dll, ntdll.dll, etc. |
Linking the C library caused even more errors which is odd as I've linked the C library before in a zig program without issue so something must have changed. a.zig:
Compiled with:
Results in these linker errors:
|
I was able to reproduce the problem. In the above linked commit I added |
The problem is still there for me. |
I suspect the problem is that *.lib files are ending up in the cwd and the linker line uses |
I also ran into this. Leftovers from a |
They're emitted into the "artifact directory" which when doing In the meantime, I think the impact of this issue was lessened by @LemonBoy removing automatic .lib file creation in favor of relying more on our bundled MinGW-w64 .lib files. |
C code (compiled as static library in visual studio 2017):
Zig code (clibtesting.zig):
Build command:
zig build-exe clibtesting.zig --library clib.lib
Error:
When running zig build-exe a kernel32.def file is created which lists some Windows API functions.
If the C code is changed to call any of those functions, the program links fine.
Changing the C code to call OutputDebugStringA results in
lld: error: undefined symbol: __imp_OutputDebugStringA
Changing the C code to call FreeLibrary results in
lld: error: undefined symbol: __imp_FreeLibrary
The text was updated successfully, but these errors were encountered: