What files are actually included when compiling

2019-04-24 23:16发布

I have a very large code, a lot of which is legacy code. I want to know which of all these files are taking part in the compilation. The code is written in GNU compilers and mostly in C/C++, but some in other programs too. Any advice will be highly appreciated.

Thanks,

Moshe.

I am compiling under linux with a mix of scripts/makefiles. I want to somehow 'wrap' this build with a tool which will give an output of all the source files used in the build, preferably with absolute path names.

What do you say?

标签: c++ c gcc g++
5条回答
叛逆
2楼-- · 2019-04-24 23:38

If you want to show included headers then whether that's supported and how to do it depends on the compiler.

E.g.,

C:\test> (g++ --help --verbose 2>&1) | find "header"
  -print-sysroot-headers-suffix Display the sysroot suffix used to find headers
  --sysroot=<directory>    Use <directory> as the root directory for headers
  -H                          Print the name of header files as they are used
  -MG                         Treat missing header files as generated files
  -MM                         Like -M but ignore system header files
  -MMD                        Like -MD but ignore system header files
  -MP                         Generate phony targets for all headers
  -Wsystem-headers            Do not suppress warnings from system headers
  -print-objc-runtime-info    Generate C header of platform-specific features
  -ftree-ch                   Enable loop header copying on trees

C:\test> (cl /? 2>&1) | find "include"
/FI<file> name forced include file      /U<name> remove predefined macro
/u remove all predefined macros         /I<dir> add to include search path
/nologo suppress copyright message      /showIncludes show include file names

C:\test> _

In the above you can see the relevant options for respectively g++ and Visual C++.

Cheers & hth.,

– Alf

查看更多
你好瞎i
3楼-- · 2019-04-24 23:41
  1. How do you build the application? I.e. what do you type at the terminal to build it?
  2. Depending on your answer to (1), find the relevant program used for the build (i.e. make, scons, etc.)
  3. Now find the input file(s) to that build program, like Makefile, SConstruct, etc.
  4. Look into this build file and other build files used by it to figure out which source files go into the build
查看更多
等我变得足够好
4楼-- · 2019-04-24 23:43

Here is a technique that finds all include files using make. It is non intrusive so you don't need to make any changes to files, or even to actually compile. Make will do all the work for you.

make -d

will run make and emit lots and lots of lines describing the inner processing of the make process. The most important is the consideration of dependencies.

Parsing the output it is easy to find the dependencies, and all other files.

Here is a Linux command line that gets a sorted list of directories that contain include files:

make -d | awk '/Prerequisite/ { if(match($2,".(.*)(/)(.*\\.h)",m)) { c[m[1]]++ ; } } END {for(d in c) print "\"" d "\",";} ' | sort

In this case the directories are quoted and a comma is added at the end, so the ouput is ready to be included in Visual Studio Code (vscode) configuration file c_cpp_properties.json

Simple variations can produce the grand list of include dependencies, like so:

make -d | awk '/Prerequisite/ { if(match($2,".(.*\\.h)",m)) { c[m[1]]++ ; } } END {for(d in c) print  d ;} ' | sort

This should also work with targets (e.g. make All)

查看更多
Juvenile、少年°
5楼-- · 2019-04-24 23:53

For a given compilation unit, e.g. foo.cpp, add the flags -E -g3 to the call of g++. This gives you the preprocessed code. There you can look which things are included.

查看更多
Viruses.
6楼-- · 2019-04-24 23:54

Two options come to mind.

  1. Parse the compilation log

    Run a build, save the log, and then search in the log.

  2. Find the files that are opened during the compilation time.

    A way to do that might be to use a system tracing tool like strace or library tracing tool like ltrace and then look out for file open calls.

    See also How can I detect file accesses in Linux?

查看更多
登录 后发表回答