compiling with clang and plugin

2020-07-18 05:20发布

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?

2条回答
Juvenile、少年°
2楼-- · 2020-07-18 06:03

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

查看更多
ら.Afraid
3楼-- · 2020-07-18 06:10

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 all Xclang are forwarded to the cc1 part of clang 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.

查看更多
登录 后发表回答