zig icon indicating copy to clipboard operation
zig copied to clipboard

lld-link: error: undefined symbol: _gai_strerrorA

Open zhaozg opened this issue 5 years ago • 6 comments

fail when zig cc -target i386-windows-gnu

zhaozg avatar Dec 15 '20 14:12 zhaozg

[nix-shell:~/dev/zig/build]$ ./zig cc -o hello hello.c -target i386-windows-gnu

[nix-shell:~/dev/zig/build]$ wine hello.exe 
Hello, World!

need more information please

andrewrk avatar Dec 15 '20 20:12 andrewrk

#include <ws2tcpip.h>
int main() {
    (void)gai_strerror(666);
    return 0;
}

LemonBoy avatar Dec 16 '20 11:12 LemonBoy

It turns out that some mingw .lib (or .a in non-Windows parlance) is actually built with a slightly more complex method than the one we use right now.

For example ws2_32.lib is built by adding (using ar cru) the .o files built from libsrc/{ ws2_32.c,ws2tcpip/*,wspiapi/* into the ws2_32.lib file produced from the .def file.

LemonBoy avatar Dec 16 '20 11:12 LemonBoy

The issue does exist, and it was an originally a bug in mingw: https://sourceforge.net/p/mingw/mailman/mingw-users/thread/da6ae25705030719572e9601f2%40mail.gmail.com/

They provide the implementation (https://github.com/mirror/mingw-w64/blob/master/mingw-w64-crt/libsrc/ws2tcpip/gai_strerrorA.c), I guess Zig should do the same.

nuald avatar Nov 21 '21 07:11 nuald

I tried to link with libcurl and linking fails due to gai_strerrorA but also some other functions:

LLD Link... lld-link -ERRORLIMIT:0 -NOLOGO -DEBUG -STACK:16777216 -MACHINE:X64 -OUT:C:\source\zig-updater\zig-cache\o\92950ba1259df601f949d702e119bc03\zig-updater.exe -IMPLIB:C:\source\zig-updater\zig-cache\o\92950ba1259df601f949d702e119bc03\zig-updater.lib -LIBPATH:deps\curl_win_mingw\lib deps\curl_win_mingw\lib\libcurl.a deps\curl_win_mingw\lib\libz.a deps\curl_win_mingw\lib\libnghttp2.a deps\curl_win_mingw\lib\libssh2.a deps\curl_win_mingw\lib\libgsasl.a deps\curl_win_mingw\lib\libssl.a deps\curl_win_mingw\lib\libcrypto.a deps\curl_win_mingw\lib\libidn2.a deps\curl_win_mingw\lib\libbrotlidec-static.a deps\curl_win_mingw\lib\libbrotlicommon-static.a deps\curl_win_mingw\lib\libnghttp3.a deps\curl_win_mingw\lib\libngtcp2.a deps\curl_win_mingw\lib\libngtcp2_crypto_openssl.a C:\source\zig-updater\zig-cache\o\92950ba1259df601f949d702e119bc03\zig-updater.obj -SUBSYSTEM:console -lldmingw -ALTERNATENAME:__image_base__=__ImageBase C:\Users\bc\AppData\Local\zig\o\6abe38eff0744be0c0064aca0f5aaf3c\crt2.obj C:\Users\bc\AppData\Local\zig\o\108341af0f7618941721253ddd883aaa\mingw32.lib C:\Users\bc\AppData\Local\zig\o\6188f47a93bdf44512181883eb811f46\mingwex.lib C:\Users\bc\AppData\Local\zig\o\4c0a651b1213b5bce9704e8341cabfa7\msvcrt-os.lib C:\Users\bc\AppData\Local\zig\o\a9a8120d60cd4590c3d569987a060287\ssp.lib C:\Users\bc\AppData\Local\zig\o\987ce507f1b926a55478374549655089\compiler_rt.lib C:\Users\bc\AppData\Local\zig\o\75ffeb15f7a3d5a3ab28078277cf50ba\gdi32.lib C:\Users\bc\AppData\Local\zig\o\ae187a8c099ae6c04db91511a8d87fed\ws2_32.lib C:\Users\bc\AppData\Local\zig\o\7d684fc7d38063ebe0fc695e4076ef7c\crypt32.lib C:\Users\bc\AppData\Local\zig\o\0b3ae7bc95595e915e43cff4944c3c72\wldap32.lib C:\Users\bc\AppData\Local\zig\o\eb4bf5cbe79bdbe04f8bd7b989b25ab8\bcrypt.lib C:\Users\bc\AppData\Local\zig\o\e656987f4825648f6515dc0df3c95d12\advapi32.lib C:\Users\bc\AppData\Local\zig\o\06ef9f195e5010a5ba65eb088be3f418\kernel32.lib C:\Users\bc\AppData\Local\zig\o\825beb5bfeb218c6585f12a3a2061d17\msvcrt.lib C:\Users\bc\AppData\Local\zig\o\42a36a000b84b043e29f827ea395fb9d\ntdll.lib C:\Users\bc\AppData\Local\zig\o\e8a77f8e09a2d58da379a90ab0f4fdf5\shell32.lib C:\Users\bc\AppData\Local\zig\o\453b365af5920b612cb8b142426804d2\user32.lib
lld-link: error: undefined symbol: WspiapiGetNameInfo
>>> referenced by libcrypto.a(libcrypto-lib-bio_addr.obj):(addr_strings)

lld-link: error: undefined symbol: gai_strerrorA
>>> referenced by libcrypto.a(libcrypto-lib-bio_addr.obj):(addr_strings)
>>> referenced by libcrypto.a(libcrypto-lib-bio_addr.obj):(BIO_lookup_ex)

lld-link: error: undefined symbol: WspiapiFreeAddrInfo
>>> referenced by libcrypto.a(libcrypto-lib-bio_addr.obj):(BIO_ADDRINFO_free)

lld-link: error: undefined symbol: WspiapiGetAddrInfo
>>> referenced by libcrypto.a(libcrypto-lib-bio_addr.obj):(BIO_lookup_ex)
error: LLDReportedFailure
zig-updater...The following command exited with error code 1:
C:\bin\zig\zig.exe build-exe C:\source\zig-updater\src\main.zig -lc -lgdi32 -lws2_32 -lcrypt32 -lwldap32 -lcurl -lz -lnghttp2 -lssh2 -lgsasl -lbcrypt -lssl -lcrypto -lidn2 -lbrotlidec-static -lbrotlicommon-static -lnghttp3 -lngtcp2 -lngtcp2_crypto_openssl --verbose-link --cache-dir C:\source\zig-updater\zig-cache --global-cache-dir C:\Users\bc\AppData\Local\zig --name zig-updater -I C:\source\zig-updater\deps\curl_win_mingw\include -L deps\curl_win_mingw\lib --enable-cache
error: the following build command failed with exit code 1:
C:\source\zig-updater\zig-cache\o\abc128821e9dbfb25105f6122f310f6e\build.exe C:\bin\zig\zig.exe C:\source\zig-updater C:\source\zig-updater\zig-cache C:\Users\bc\AppData\Local\zig --verbose-link

Looking at the ws2_32.def file within the cache folder (C:\Users\bc\AppData\Local\zig\o\ae187a8c099ae6c04db91511a8d87fed) shows that those functions are indeed missing from ws2_32.lib.

BuzzwordChief avatar May 31 '22 17:05 BuzzwordChief

I am facing the same issue right now. Is there any workaround for this currently?

eLeCtrOssSnake avatar Jul 26 '22 19:07 eLeCtrOssSnake

I am also facing this issue. Is there any workaround for the short term?

Mehgugs avatar Nov 23 '22 08:11 Mehgugs

Fixed in HEAD.

zhaozg avatar May 12 '23 14:05 zhaozg

I'm running into this issue on a newer zig version. I'm not sure it actually was fixed.

// test.c
#include <ws2tcpip.h>
int main() {
    (void)gai_strerror(666);
    return 0;
}
$ zig cc test.c -lws2_32 -target x86_64-windows-gnu
LLD Link... lld-link: error: undefined symbol: gai_strerrorA
>>> referenced by C:\Users\pocke\projects\test.c:3
>>>               C:\Users\pocke\AppData\Local\zig\o\a1ebd165a3cdd595ddb331e96790a366\test.obj:(main)

$ zig version
0.12.0-dev.799+d68f39b54

Parzival-3141 avatar Oct 10 '23 01:10 Parzival-3141

hit this too, my current workaround:

const std = @import("std");
const builtin = @import("builtin");
const windows = std.os.windows;
const WCHAR = windows.WCHAR;

const in6addr_any = [_]u8{0} ** 16;
const GAI_STRERROR_BUFFER_SIZE = 1024;

comptime {
    if (builtin.os.tag == .windows) {
        @export(in6addr_any, .{ .name = "in6addr_any" });
        @export(gai_strerrorA, .{ .name = "gai_strerrorA" });
    }
}

inline fn MAKELANGID(p: c_ushort, s: c_ushort) windows.LANGID {
    return (s << 10) | p;
}

fn gai_strerrorW(ecode: c_int) callconv(.C) [*c]WCHAR {
    const static = struct {
        var buf: [GAI_STRERROR_BUFFER_SIZE + 1]WCHAR = [_]WCHAR{0} ** (GAI_STRERROR_BUFFER_SIZE + 1);
    };
    var len = windows.kernel32.FormatMessageW(
        windows.FORMAT_MESSAGE_FROM_SYSTEM |
            windows.FORMAT_MESSAGE_IGNORE_INSERTS |
            windows.FORMAT_MESSAGE_MAX_WIDTH_MASK,
        null,
        @enumFromInt(ecode),
        MAKELANGID(windows.LANG.NEUTRAL, windows.SUBLANG.DEFAULT),
        @ptrCast(&static.buf),
        GAI_STRERROR_BUFFER_SIZE,
        null,
    );
    std.debug.assert(len < GAI_STRERROR_BUFFER_SIZE);
    return @ptrCast(&static.buf);
}

fn gai_strerrorA(ecode: c_int) callconv(.C) [*c]u8 {
    const static = struct {
        var buf: [GAI_STRERROR_BUFFER_SIZE + 1]u8 = [_]u8{0} ** (GAI_STRERROR_BUFFER_SIZE + 1);
    };
    var len = std.unicode.utf16leToUtf8(&static.buf, std.mem.span(gai_strerrorW(ecode))) catch
        return null;
    std.debug.assert(len < GAI_STRERROR_BUFFER_SIZE);
    return @ptrCast(&static.buf);
}

binarycraft007 avatar Oct 12 '23 09:10 binarycraft007

My workaround is adding the missing *.a lib files: zig cc -o test.exe .\test.c C:\msys64\mingw64\lib\libws2_32.a -lws2_32 -Dtarget=x86_64-windows-gnu

abjmakes avatar Dec 02 '23 07:12 abjmakes