Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 246
Wes Miller Avatar asked Dec 09 '13 17:12

Wes Miller


People also ask

How do I list files in a directory with full path?

Listing the full path The command DIR /b will return just a list of filenames, when displaying subfolders with DIR /b /s the command will return a full pathname. To list the full path without including subfolders, use the WHERE command.

How do I see all paths in Linux?

How can you tell in which directory you're currently working? The answer is the pwd command, which stands for print working directory. The word print in print working directory means “print to the screen,” not “send to printer.” The pwd command displays the full, absolute path of the current, or working, directory.

How do I find my full path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I get the full path in bash?

In bash, there are a number of ways of retrieving the full address of a script. In particular, we can use realpath, readlink, or even create our custom little script. When we want to know the directory path, we can use the dirname command in our bash script to retrieve our directory path.


2 Answers

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.

You could also consider developing your GCC plugin to register these headers somewhere (e.g. in your sqlite database), perhaps inspired by this draft report, or the CHARIOT or DECODER European projects. You could also consider using, or extending, the Clang static analyzer.

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

like image 171
Basile Starynkevitch Avatar answered Oct 22 '22 13:10

Basile Starynkevitch


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).

like image 44
Oliver Matthews Avatar answered Oct 22 '22 14:10

Oliver Matthews