compiling with clang and plugin

纵饮孤独 提交于 2019-12-07 15:03:37

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/43667112/compiling-with-clang-and-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!