[英]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: 并获得:
and for 和为
.getAllIncludes main.cpp -I includepath1;includepath2
I'll obtain: 我将获得:
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.