Search code examples
clangllvm-clang

Clang Libtool behavior different based on the location of the generated binary


I have created a very simple Clang Libtool based program which builds an AST and dumps the AST to the console.

I'm compiling/linking my program which is present outside the LLVM/Clang source tree against one of the pre-built binaries from http://releases.llvm.org/download.html using the following commands -

c++ -DCLANG_ENABLE_ARCMT -DCLANG_ENABLE_OBJC_REWRITER -DCLANG_ENABLE_STATIC_ANALYZER -DGTEST_HAS_RTTI=0 -DLLVM_BUILD_GLOBAL_ISEL -D_DEBUG -D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/llvm-3.9.1/clang+llvm-3.9.1-x86_64-linux-gnu-debian8/include -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -Werror=date-time -std=c++11 -fno-common -Woverloaded-virtual -fno-strict-aliasing -g -fno-exceptions -fno-rtti -o main.cpp.o -c Main.cpp

Then linking using the following command -

c++ -fPIC -fvisibility-inlines-hidden -Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wno-maybe-uninitialized -Wdelete-non-virtual-dtor -Wno-comment -Werror=date-time -std=c++11 -fno-common -Woverloaded-virtual -fno-strict-aliasing -g -Wl,-allow-shlib-undefined -Wl,-rpath-link,/llvm-3.9.1/clang+llvm-3.9.1-x86_64-linux-gnu-debian8/lib main.cpp.o -o myprogram -Wl,-rpath,/llvm-3.9.1/clang+llvm-3.9.1-x86_64-linux-gnu-debian8/lib -L/llvm-3.9.1/clang+llvm-3.9.1-x86_64-linux-gnu-debian8/lib -lpthread -lclangTooling -lclangBasic -lclangASTMatchers -lclangFormat -lclangFrontend -lclangDriver -lclangParse -lLLVMMCParser -lclangSerialization -lclangSema -lclangEdit -lclangAnalysis -lLLVMBitReader -lLLVMProfileData -lclangToolingCore -lclangAST -lclangRewrite -lclangLex -lclangBasic -lLLVMCore -lLLVMMC -lLLVMOption -lLLVMSupport -lrt -ldl -ltinfo -lpthread -lz -lm

I see no errors or issues for the above 2 steps.

If I copy this myprogram binary to the /llvm-3.9.1/clang+llvm-3.9.1-x86_64-linux-gnu-debian8/bin directory and run it the AST gets generated properly and runs as expected.

But if I run this binary from any other location, I see no warnings or errors but the AST is not getting generated. The program dumps an incomplete AST which is generated after the pre-processing stage.

I have tried to use the -static flag while linking and also set the ${LD_LIBRARY_PATH} value to the /llvm-3.9.1/clang+llvm-3.9.1-x86_64-linux-gnu-debian8/lib folder but this does not make any difference.

Since the program runs properly when it is moved to the llvm/bin folder but does not run properly when it is present anywhere else. It looks like the program loads some libraries which are relative to the /bin directory but I am unable to figure out what needs to be set to ensure that the program runs properly which it is moved anywhere else.

Please can anyone provide any suggestions on how to proceed. Thanks.


Solution

  • The issue here was that after compiling/linking the Clang Libtool program, when we execute the program we need to provide the -isystem flag to the executable pointing to the llvm version's lib folder in the following manner -

    $ ./myprogram -isystem/{CLANG_PATH}/lib "other args"
    

    With llvm-5.0 I noticed that using the -static keyword while linking leads to a warning about glib version similar to this stackoverflow post.

    I was able to resolve this by setting the -rpath flag correctly as below in my Makefile-

    "-Wl,-rpath,$(CLANG_PATH)/lib"