Zig C++: Undefined Symbols When Linking C++20 Shared Library

Greetings,

I made a post a while ago about issues with using Zig’s c++ compiler to compile a sample program that utilizes the ROOT Analysis Framework libraries. I have updated the zig cpp build template I was using to follow 0.15.1 standards, but I am still having troubles.

Looking into the problem I thought that maybe it was due to the compiler and version with which ROOT’s shared libraries were compiled (by default it was gcc) so I compiled a local version of root using Clang++ 20.1.8 and tried using those header files instead. Unfortunately this did not seem to address the issues.

I have confirmed that the program code works by running:

clang++ -pthread -std=c++20 -m64 -I/home/username/src/.commits/cpp-build-template/src/cpp/../../../../root_src/install/include main.cpp -o main -L/home/username/src/root_src/install/lib -lCore -lImt -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lGenVector -lROOTVecOps -lMathCore -lTree -lTreePlayer -lRint -lPostscript -lMatrix -lPhysics -lThread -lROOTNTuple -lMultiProc -lROOTDataFrame -lROOTNTupleUtil -Wl,-rpath,/home/username/src/root_src/install/lib -pthread -lm -ldl -rdynamic

Trying to compile main.cpp with either the build system or by replacing clang++ with zig c++ results in the error messages I have attached here (this has been done with both 0.15.1 and with master).

The code may be found at this github (be warned, though I have compressed the relevant library and header files into zip folders, it is currenly a somewhat large repo to download).

Any help would be appreciated.

inlineErr.txt (43.4 KB)
zigbuilderr.txt (25.4 KB)

This is probably due to a mismatched C++ stdlib, as both gcc and clang will build against libstdc++ by default, but zig c++ and the buildsystem will use the bundled libc++ by default which produces different symbol names.

If this is the case, the easiest solution is to rebuild ROOT against libc++ using the -stdlib=libc++ flag (or ideally you would use zig c++ instead of clang). Alternatively, you could try setting appropriate flags for zig to use the system libstdc++ instead of libc++ (this thread might be helpful, although last I tried I couldn’t get it to work and build my project :frowning:)

To add here, even though clang / gcc aim to be compatible, using different compilers (versions and compiler settings) may still produce C++ code that is incompatible over ABI boundary as C++ doesn’t really have a stable ABI.

…and adding to that, compiling a C++ library into shared libraries is generally a bad idea, unless the C++ library is wrapped in a plain old C API.

Thanks for all the comments so far, I am currently trying to build ROOT with libc++ (though explicitly supported by the project, there is currently an issue on the github for using clang++ with libc++). I have also tried to force build.zig to use libstdc++, but it still complains (i.e. it refuses to use libstdc++).

Also, for those that don’t know, ROOT is CERN’s physics and data analysis framework and is quite large, so it builds into a bunch of shared libraries (with additional support for python, fortran, and julia)