Skip to content
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

Program built by Zig can't link correctly against system LLVM libraries in Arch Linux #19275

Open
david4r4 opened this issue Mar 12, 2024 · 6 comments
Labels
bug Observed behavior contradicts documented or intended behavior os-linux
Milestone

Comments

@david4r4
Copy link
Contributor

david4r4 commented Mar 12, 2024

Zig Version

0.12.0-dev.3191+9cf28d1e9

Steps to Reproduce and Observed Behavior

> uname -a
Linux minipc-linux 6.6.21-1-lts #1 SMP PREEMPT_DYNAMIC Wed, 06 Mar 2024 16:59:55 +0000 x86_64 GNU/Linux

Observe birth-software/nativity#112
I get these linking errors: https://zigbin.io/d2c32c

Relevant part of the build.zig code:

    compiler.linkLibC();
    compiler.linkLibCpp();
    compiler.linkSystemLibrary("LLVM");
    compiler.linkSystemLibrary("clang");
    compiler.linkSystemLibrary("clang-cpp");
    compiler.linkSystemLibrary("lldCommon");
    compiler.linkSystemLibrary("lldCOFF");
    compiler.linkSystemLibrary("lldELF");
    compiler.linkSystemLibrary("lldMachO");

Am I doing anything wrong? Or is it just impossible to work with C++ system libraries from Zig. I tried with Clang and it works...

Expected Behavior

To be able to compile and link correctly.

@david4r4 david4r4 added the bug Observed behavior contradicts documented or intended behavior label Mar 12, 2024
@nektro
Copy link
Contributor

nektro commented Mar 12, 2024

these seem much more relevant

error: unable to find Dynamic system library 'lldCommon' using strategy 'paths_first'. searched paths:
error: unable to find Dynamic system library 'lldCOFF' using strategy 'paths_first'. searched paths:
error: unable to find Dynamic system library 'lldELF' using strategy 'paths_first'. searched paths:
error: unable to find Dynamic system library 'lldMachO' using strategy 'paths_first'. searched paths:

I would double check that llvm is visible to your build

@david4r4
Copy link
Contributor Author

these seem much more relevant

error: unable to find Dynamic system library 'lldCommon' using strategy 'paths_first'. searched paths:
error: unable to find Dynamic system library 'lldCOFF' using strategy 'paths_first'. searched paths:
error: unable to find Dynamic system library 'lldELF' using strategy 'paths_first'. searched paths:
error: unable to find Dynamic system library 'lldMachO' using strategy 'paths_first'. searched paths:

I would double check that llvm is visible to your build

As I said, using Clang works just fine:

clang src/llvm/clang_cc1.cpp src/llvm/clang_main.cpp src/llvm/clang_cc1as.cpp src/llvm/ar.cpp src/llvm/lld.cpp src/llvm/llvm.cpp -lc -lstdc++ -lLLVM -lclang -lclang-cpp -llldCommon -llldCOFF -llldELF -llldMachO -llldWasm
/usr/bin/ld: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../lib64/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

@nektro
Copy link
Contributor

nektro commented Mar 12, 2024

clang: error: [...]

no it didn't ?

@david4r4
Copy link
Contributor Author

Yes, it did. It just doesn't find the main entry point (which is normal because I don't have an entry point in those files), it can resolve the LLVM symbols just fine.

@david4r4
Copy link
Contributor Author

So after trying to understand what Zig does to link against libstdc++, I came out with something that works:

    compiler.addObjectFile(.{ .cwd_relative = "/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../lib/libstdc++.so" });
    compiler.addIncludePath(.{ .cwd_relative = "/usr/include"});
    compiler.addIncludePath(.{ .cwd_relative = "/usr/include/c++/13.2.1"});
    compiler.addIncludePath(.{ .cwd_relative = "/usr/include/c++/13.2.1/x86_64-pc-linux-gnu"});
    compiler.addLibraryPath(.{ .cwd_relative = "/usr/lib"});

The solution is not ideal and probably the best thing is to let the user link against libstdc++ without these workarounds, unless I am missing something?

@andrewrk andrewrk added this to the unplanned milestone Aug 15, 2024
@andrewrk
Copy link
Member

You can't mix different libc++

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior os-linux
Projects
None yet
Development

No branches or pull requests

3 participants