开发者

How to create a non-transitive dependency list (GCC)

开发者 https://www.devze.com 2023-02-14 22:10 出处:网络
Is there a way to generate dependencies of a C(++) source file similar to using the -MM option of GCC that only include the direct dependencies of said file, i.e. only the files directly included by t

Is there a way to generate dependencies of a C(++) source file similar to using the -MM option of GCC that only include the direct dependencies of said file, i.e. only the files directly included by this source file?

More context on why I'm looking for this functionality - maybe there is a completely different solution to my problem: I have a generic makefile with auto-detection of dependencies that suffices my needs but is slow. The basic structure is as follows:

  • Full dependencies of main.cpp are retrieved with gcc -MM
  • All *.h dependencies for which a corresonding *.cpp exists are changed to *.o dependencies
  • the altered dependencies are included in the makefile
  • All *.o targets are built, dependencies are retrieved with gcc -MM and included
  • All *.o targets are linked to create the executable

So far, this makefile has worked fine but -as said before- it is slow. I analyzed its execution path for one project and included all the generated dependencies by hand to try and optimize its speed. The result was by removing all transitive开发者_开发百科 dependencies, the makefile retained its functionality but got much faster (also reflected in the number of lines of the debug output of make -d).


First of all, the method you are using is slightly confusing. All .h files used in the compilation of one .cpp file must be kept in its dependencies, and the automatic collection of *.o files shouldn't be that slow. I'd advise to go with classic -MM and to build the list of cpp files that are to be compiled by hand. automake does it that way, and if there was a really reliable way of figuring out the list of compilation units automatically, these guys would have found it :-).

Nevertheless, the -H option of the gcc helps you. It prints the names of all used files to stderr, with . prefixed for the level of inclusion. So,

cpp -I $< >/dev/null | sed -n -e 's/^\. //p'

should do the trick. Caveat: If a header file is included deeper in the hierarchy first and then later in the main file, it is not found. However, your dependency system should be able to handle that if you keep the 1-to-1 of .h and .cpp files.

0

精彩评论

暂无评论...
验证码 换一张
取 消