compor / llvm-ir-cmake-utils

LLVM IR CMake utils for bitcode file manipulation by opt and friends
GNU Lesser General Public License v2.1
68 stars 14 forks source link

Linking a LLVMIR library #21

Closed jgollenz closed 1 year ago

jgollenz commented 1 year ago

I am trying to link a static library that was created with llvmir_attach_library to a target that then in turn goes through the llvmir stages itself. However, it fails when trying to link the library:

/usr/bin/ld: lib/libbar_bc_lib.a: error adding symbols: archive has no index; run ranlib to add one
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/CMakeFiles/foo.dir/build.make:98: src/foo] Error 1
make[1]: *** [CMakeFiles/Makefile2:367: src/CMakeFiles/foo.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

What I noticed, is that the object file bar.cpp.o is missing. But when I modify the src/lib/CMakeLists.txt, so it can be treated as its own cmake project, everything works fine and the object files are present. Any idea what I might be doing wrong? This is the commit with the example I added

jgollenz commented 1 year ago

Linking to the plain library bar results in a working executable. And I cross-checked with example02 (from which I copied most of the files): running ar -t libbar_bc_lib.a in both cases returns bar.bc, which in turn results in file format not recognized when running nm on the library.

jgollenz commented 1 year ago

Alright, I found llvmir_attach_obj_target which did the trick. I'll make a PR to add that to the README if you don't mind. Case closed

compor commented 1 year ago

Hey there @jgollenz, thanks for this. I have limited bandwidth ATM for this, but a PR would be obviously welcome. I'll properly look at this whenever I find some time.