简体   繁体   English

考虑预处理器定义,获取cpp文件的所有包含文件(快速)

[英]Get all include files of a cpp file considering preprocessor defines (fast)

I need a tool (command line, script or source code) that extracts all inlcude files that are included by a source file (recursive) with given preprocessor defines and include paths. 我需要一个工具(命令行,脚本或源代码)来提取具有给定预处理器定义并包含路径的源文件(递归)所包含的所有inlcude文件。 I want to know the ones that could be found and the one that doesn't. 我想知道可以找到的,而找不到的。 The include files that could be found shall be recursivly parsed. 可以找到的包含文件应递归解析。

I know about the 我知道

    gcc -M /-MM       
    cl /P

solution, but this does not work for me. 解决方案,但这对我不起作用。 The preprocessor stops as soon as it could not open a file. 一旦无法打开文件,预处理器就会停止。 But at this time I don't have the correct path for that files and just want the preprocessor to skip that file and to tell me that it could not include that file 但是目前我没有该文件的正确路径,只是希望预处理器跳过该文件并告诉我它不能包含该文件

Also the cinclude2dot.pl from here is not useful, because it seems not to consider given preprocessor defines. 同样, 此处cinclude2dot.pl没用 ,因为似乎没有考虑给定的预处理程序定义。

Very useful is the include file hierarchy finder from CodeProject . CodeProject包含文件层次结构查找器非常有用。 It considers the preprocessor flags and shows me all include files. 它考虑了预处理器标志,并向我显示了所有包含文件。 Even the includes that couldn't be opened. 甚至无法打开的包含。 But it is written in MFC and I would have to reimplement this for the gcc what is not such simple because a lot of WinAPI stuff is used even inside the parser. 但这是用MFC编写的,因此我必须为gcc重新实现它,这并不是那么简单,因为即使在解析器内部也使用了许多WinAPI东西。

Thus, maybe some one knows another solution. 因此,也许有人知道另一种解决方案。

an simple example: 一个简单的例子:

main.cpp main.cpp

#include <iostream>
#include <string>
#include <boost/foreach.hpp>

#include <SharedClass.h>
#include "MyClass.h"

#ifdef FOO
    #include <OptClass.h>
#endif

int main() {}

Right now I start the include extraction like (simplified): 现在,我开始进行包含提取,例如(简化):

.getAllIncludes main.cpp -I includepath1;includepath2 -PD FOO

and obtain: 并获得:

  • cannot open //-> I don't care right now, it's a default header 无法打开//->我现在不在乎,这是默认标头
  • cannot open // -> here I can extract the info that I need boost 无法打开//-->在这里我可以提取需要增强的信息
  • SharedClass.h 共享类
  • SharedDependenyClass.h //a header that is included by SharedClass... SharedDependenyClass.h // SharedClass包含的标头...
  • MyClass.h MyClass.h
  • TestClass.h //a header that is included by the MyClass header... TestClass.h // MyClass标头中包含的标头...
  • TestClass2.h //a header that is included by the TestClass header... TestClass2.h // TestClass头包含的头...
  • OptClass.h OptClass.h

and for 和为

.getAllIncludes main.cpp -I includepath1;includepath2 

I'll obtain: 我将获得:

  • cannot open //-> I don't care right now, it's a default header 无法打开//->我现在不在乎,这是默认标头
  • cannot open // -> here I can extract the info that I need boost 无法打开//-->在这里我可以提取需要增强的信息
  • SharedClass.h 共享类
  • SharedDependenyClass.h //a header that is included by SharedClass... SharedDependenyClass.h // SharedClass包含的标头...
  • MyClass.h MyClass.h
  • TestClass.h //a header that is included by the MyClass header... TestClass.h // MyClass标头中包含的标头...
  • TestClass2.h //a header that is included by the TestClass header... TestClass2.h // TestClass头包含的头...

I know that the deafault header may also define some values. 我知道默认的头文件可能还会定义一些值。 But in my case I don't need that information, because the project source code doesn't depend on any of that defines. 但就我而言,我不需要这些信息,因为项目源代码不依赖于任何定义。 If thus, I feed my tool with this preprocessor define... 如果是这样的话,我用这个预处理器定义来喂我的工具...

In the end the tool works quite well. 最后,该工具运行良好。 It runs recursivly over ALL necessary files and in the end I have all needed files for the project. 它以递归方式运行所有必需的文件,最后我拥有了该项目所需的所有文件。 Of course there are some small restrictions I don't want to name then all (eg every header of an source file name has the same name, ... ). 当然,还有一些我不想命名的小限制(例如,源文件名的每个标头都具有相同的名称,...)。

Using gcc -M <source_file> , the code is not compiled, it is only processed by the precompiler. 使用gcc -M <source_file> ,不编译代码,仅由预编译器处理。 And, any solution you may find needs to process the source using the precompiler, to be correct. 而且,您可能会发现,任何解决方案都需要使用预编译器来处理源,这是正确的。 Imagine that the source, somewhere, has the following snipset: 想象一下,该源在某处具有以下片段:

#ifdef USE_BOOST_SUPERLIB
#  include <boost/superlib.hpp>
#endif

then without preprocessing you cannot know if <boost/superlib.hpp> is included. 那么如果没有预处理,您将无法知道是否包含<boost/superlib.hpp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 #include 所有 .cpp 文件到一个编译单元? - #include all .cpp files into a single compilation unit? 如果前者只提供后者定义的声明,我是否应该在相关的 cpp 文件中包含 header ? - Is there any reason I should include a header in the associated cpp file if the former only provides declarations the latter defines? 所有#include-ed的“ .h”头文件都必须与.cpp文件位于同一文件夹中吗? - Do all “.h” header files that are #include-ed have to be in the same folder as your .cpp file? 包含文件的预处理器定义 - include file with preprocessor define GNU将所有.cpp文件编译为.o并包含.h文件 - GNU make compiling all .cpp files to .o and include .h files 包含.cpp标准的文件 - Include files in .cpp standard 在源文件中获取对特定函数的所有调用并生成其他文件(使用C,C ++预处理器或脚本) - Get all calls to specific function in source file and generate other files (using C, C++ preprocessor or scripts) 在这种情况下,如何包含所有.cpp和头文件? - How do I include all .cpp and header files in this situation? 如何在Visual Studio中的所有cpp文件中自动包含标头? - How to include an header automatically in all cpp files in visual studio? #include .h或.cpp文件? - #include .h or .cpp file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM