Compiling with clang and plugin

clang

supports plugins and this concept is often used to build tools like static analysis, etc. To start playing with it, I took this example , which prints all the function names present in the target cpp file. S. I have compiled the plugin by doing the following:

clang++ -v -std=c++11 PrintFunctionNames.cpp \
 $(llvm-config --cxxflags --ldflags) \
 -o plugin.so -shared -Wl,-undefined,dynamic_lookup

      

and then run it "by book":

clang++ \
 -c main.cpp \
 -Xclang -load \
 -Xclang $PWD/plugin.so \
 -Xclang -plugin \
 -Xclang print-fns

      

it works fine: it prints the function names in main.cpp and exit (without compiling main.cpp because of the -c flag).

What I would like to do is print all the function names and compile main.cpp into an executable file.
I tried to remove the flag -c

, but I got:

/usr/bin/ld: cannot find /tmp/main-284664.o: No such file or directory

      

What am I doing wrong?

+3


source to share


2 answers


I've always thought it was "natural" to run twice clang

, but that's the right question.

I don't think you are doing anything wrong, but I believe it is (not digging too much in the sources clang

) that everyone is Xclang

sent to the cc1

part clang

that creates temporary files to host the plugins. However, when the linker is called as a separate process, these files are no longer there, hence the error.
You can see it all by using the option -v

for all of these commands.



I'm not sure if this is possible, but this SO thread might provide a clue in the right direction.

+2


source


You need to use -add-plugin instead of -plugin



+1


source







All Articles