Skip to content

Link error when calling Windows API functions from function in C static library #2073

Open
@ghost

Description

C code (compiled as static library in visual studio 2017):

#include <Windows.h>
void load_library() {
	LoadLibrary("opengl32.dll");
}

Zig code (clibtesting.zig):

extern fn load_library() void;
pub fn main() void {
    load_library();
}

Build command:
zig build-exe clibtesting.zig --library clib.lib

Error:

lld: error: undefined symbol: __imp_LoadLibraryA
referenced by c:\users\dabbo\programming\zig\clibtesting\clib\clib\clib.c:3
clib.lib(clib.obj):(load_library)

When running zig build-exe a kernel32.def file is created which lists some Windows API functions.

LIBRARY kernel32
EXPORTS
ExitProcess
GetModuleFileNameW
GetLastError
GetStdHandle
SetFilePointerEx
GetCurrentDirectoryW
GetFileSizeEx
GetModuleHandleW
GetConsoleScreenBufferInfo
SetConsoleTextAttribute
ReadFile
HeapReAlloc
CreateFileW
GetEnvironmentVariableW
WriteFile
HeapCreate
HeapDestroy
HeapAlloc
CloseHandle
GetFileInformationByHandleEx
GetConsoleMode

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorenhancementSolving this issue will likely involve adding new logic or components to the codebase.os-windows

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions