clang
supports plugins, and often this concept is used to build tools like static analysis and such.
To start playing with it I took this example which prints all function names present in the target cpp file(s).
I compiled the plugin running 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 the book":
clang++ \
-c main.cpp \
-Xclang -load \
-Xclang $PWD/plugin.so \
-Xclang -plugin \
-Xclang print-fns
it works just fine: it prints the function names in main.cpp
and exit (without compiling main.cpp due the -c flag).
What I'd like to do is to print all the function names AND compile main.cpp into an executable.
I tried removing the -c
flag but I got:
/usr/bin/ld: cannot find /tmp/main-284664.o: No such file or directory
What am I doing wrong?
You need to use -add-plugin instead of -plugin
I always thought it "natural" to run
clang
twice, but it is a valid question.I don't think you are doing anything wrong, but I believe (didn't dig too much into
clang
sources) what is happening is that allXclang
are forwarded to thecc1
part ofclang
which create temp files to accommodate the plugin runs. However, when the linker is evoked as a separate process those files are not there anymore, hence the error.You can see all that by using
-v
option on all those commands.I'm not sure if this possible, but this SO thread might provide a clue to the right direction.