How To Get g++ to list paths to all #included files

最后都变了- 提交于 2019-12-09 12:56:16

问题


I would like to have g++/gcc tell me the paths to everything non-system it is #include-ing in C++ build. Turns out, that is a tough search as Google mus-interprets it about ten different ways.

I want these filenames and paths so I can add them to the search path for Exuberant CTAGS. We have a huge project and if I use ctags on the whole thing it takes about half an hour to generate the tags file and nearly as long for the editor to do a look-up.

We use CMakeLisats to do the compiling. If there is a directive I can paste into the CMakeLists.txt, that would be extra wonderfulness.

I don't really need the default paths and filenames, Johnathan Wakely gave a good tool for that here. I think that pretty much covers the fact that this is a cross compile job. I don't need the cross-system files either.


回答1:


You need to invoke g++ with the -M option.

From the manual:

Instead of outputting the result of preprocessing, output a rule suitable for make describing the dependencies of the main source file. The preprocessor outputs one make rule containing the object file name for that source file, a colon, and the names of all the included files, including those coming from -include or -imacros command line options.

It's worth reading the manual to consider the other -M sub options (-MM and -MF in particular may be of use).




回答2:


Try gcc or g++ with the -H option (to the preprocessor part of it). From the doc:

-H

Print the name of each header file used, in addition to other normal activities. Each name is indented to show how deep in the ‘#include’ stack it is. Precompiled header files are also printed, even if they are found to be invalid; an invalid precompiled header file is printed with ‘...x’ and a valid one with ‘...!’ .

It tells you all the headers which are included. You may filter out (with grep -v or awk) those that you don't want.

In contrast to the -M options suggested in Oliver Matthews' answer, it does not tell you more (but gives all the included files).



来源:https://stackoverflow.com/questions/20476377/how-to-get-g-to-list-paths-to-all-included-files

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