C++ 跟踪#include 依赖项的工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tool to track #include dependencies
提问by Agnel Kurian
Any good suggestions? Input will be the name of a header file and output should be a list (preferably a tree) of all files including it directly or indirectly.
有什么好的建议吗?输入将是头文件的名称,输出应该是直接或间接包含它的所有文件的列表(最好是树)。
采纳答案by KeithB
If you have access to GCC/G++, then the -M
optionwill output the dependency list. It doesn't do any of the extra stuff that the other tools do, but since it is coming from the compiler, there is no chance that it will pick up files from the "wrong" place.
如果您有权访问 GCC/G++,则该-M
选项将输出依赖项列表。它不会执行其他工具所做的任何额外工作,但由于它来自编译器,因此它不可能从“错误”的地方获取文件。
回答by Agnel Kurian
Thanks to KeithB. I looked up the docs for cl.exe (VS2008) and found the /showIncludes flag. From the IDE, this can be set from the property page of any CPP file.
感谢 KeithB。我查找了 cl.exe (VS2008) 的文档并找到了 /showIncludes 标志。在 IDE 中,这可以从任何 CPP 文件的属性页进行设置。
回答by Matt Dillard
For a heavy weight solution, you should check out doxygen. It scans through your code base and comes up with a website, effectively, that documents your code. One of the many things it shows is include trees.
对于重量级解决方案,您应该查看doxygen。它会扫描您的代码库并创建一个网站,有效地记录您的代码。它显示的众多事物之一是包括树木。
If you were looking to be able to plug the output of this tool into some other process, then this may not work for you (although doxygen does output to other formats, I'm not real familiar with that feature). If you simply want to eyeball the dependencies, though, it should work great.
如果您希望能够将这个工具的输出插入到其他一些进程中,那么这可能对您不起作用(尽管 doxygen 确实输出为其他格式,但我并不熟悉该功能)。但是,如果您只是想查看依赖项,它应该会很好用。
回答by Brian Stewart
I've played around with a tool called cinclude2dot. It was pretty useful in getting a handle on a rather large codebase when I came to work here. I've actually thought about integrating it into our daily build eventually.
我玩过一个叫做cinclude2dot的工具。当我来到这里工作时,它对于处理相当大的代码库非常有用。我实际上已经考虑过最终将它集成到我们的日常构建中。
回答by Agnel Kurian
Good news: redhat Source-Navigator(runs on Windows too). Of course, compiler switches (mentioned earlier) have superior parsing and I'm not sure how this will handle MFC, Qt and their magic keywords.
好消息:redhat Source-Navigator(也可以在 Windows 上运行)。当然,编译器开关(前面提到过)具有出色的解析能力,我不确定这将如何处理 MFC、Qt 及其魔法关键字。
回答by Allbite
First, cinclude2dot.pl is a perl script which analyses C/C++ code and produces a #include dependency graph as a dot file for input into graphviz.
首先,cinclude2dot.pl 是一个 perl 脚本,它分析 C/C++ 代码并生成#include 依赖关系图作为点文件以输入到 graphviz。
http://www.flourish.org/cinclude2dot/
http://www.flourish.org/cinclude2dot/
If you don't want to go the way of that sort of manual tool, then the hands-down by far winner is in my opinion a tool known as "IncludeManager" from ProFactor.
如果您不想走那种手动工具的方式,那么在我看来,迄今为止最成功的工具是来自 ProFactor 的称为“IncludeManager”的工具。
http://www.profactor.co.uk/includemanager.php
http://www.profactor.co.uk/includemanager.php
There's a free trial, and it is awesome. It's a plug-in for Visual Studio that's totally integrated so double clicking on something over here takes you to the place where it is included over there.
有免费试用,很棒。它是一个完全集成的 Visual Studio 插件,因此双击此处的某些内容会将您带到包含它的地方。
Tooltip mouseovers give you all the info you would want, and it lets you drill down / up, remove whole subtrees you don't care about, view representations other than graphs, cycle through a list of matches for this and that, it's wonderful.
工具提示鼠标悬停为您提供您想要的所有信息,它可以让您向下/向上钻取,删除您不关心的整个子树,查看除图表之外的表示,循环浏览匹配项列表,这太棒了。
If you're quick about it, you can refactor the #include structure of a large projects before the trial runs out. Even so, it doesn't cost much, about $35 per license.
如果您很快,您可以在试用期结束之前重构大型项目的#include 结构。即便如此,它的成本也不高,每个许可证大约 35 美元。
For what it does, it is just about perfect. Not only #include graphs but also cross project dependencies of shared files, impact on build times, detailed properties in grids, perfect.
对于它所做的,它几乎是完美的。不仅是#include 图表,还有共享文件的跨项目依赖,对构建时间的影响,网格中的详细属性,完美。
回答by Allbite
Building on KeithB's answer, here is GNUmake syntax to automatically 1) generate the dependency files, 2) keep them up to date, and 3) use them in your makefile:
基于KeithB 的回答,这里是 GNUmake 语法,可自动 1) 生成依赖项文件,2) 使它们保持最新,以及 3) 在您的 makefile 中使用它们:
.dep:
mkdir $@
.dep/%.dep: %.c .dep
(echo $@ \; $(CC) $(IFLAGS) -MM $<) > $@ || (rm $@; false)
.dep/%.dep: %.cpp .dep
(echo $@ \; $(CXX) $(IFLAGS) -MM $<) > $@ || (rm $@; false)
DEPEND := $(patsubst %.dep,.dep/%.dep,$(OBJ:.o=.dep))
-include $(DEPEND)
(Make sure to change those indents to hardtabs.)
(确保将这些缩进更改为硬制表。)
回答by dwj
You can also check out makedepend:
您还可以查看makedepend:
http://en.wikipedia.org/wiki/Makedepend
http://en.wikipedia.org/wiki/Makedepend
回答by Xavier Nodet
Understand for C++should be able to help you: it builds a database that you can access from Perl.
理解 C++应该能够帮助你:它构建了一个你可以从 Perl 访问的数据库。
回答by svec
cscope (http://cscope.sourceforge.net/) does this in a standalone xterm, and also can be used inside your favorite editor - it has great emacs and vi/vim support.
cscope ( http://cscope.sourceforge.net/) 在一个独立的 xterm 中做到这一点,也可以在你最喜欢的编辑器中使用 - 它有很好的 emacs 和 vi/vim 支持。