What files are actually included when compiling

ぃ、小莉子 提交于 2020-01-02 02:31:05

问题


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?


回答1:


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?




回答2:


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




回答3:


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.




回答4:


  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



回答5:


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)



来源:https://stackoverflow.com/questions/3900874/what-files-are-actually-included-when-compiling

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