【问题标题】:Getting source file name which contains definition of entity declared in a header file?获取包含在头文件中声明的实体定义的源文件名?
【发布时间】:2015-05-10 12:27:53
【问题描述】:

我有 2 个源文件:test.cpp and func.cpp 和1个头文件:func.h

这些文件的内容如下:

1。 test.cpp

#include "func.h"

int main() {
 ...
 func();
 ...
}

2。 func.cpp

#include "func.h"

void func() {
 //func() definition
}

======================

  1. func.h

======================

#include <iostream>
using namespace std;

void func(); /* func() declaration */

======================

问题: 问题是找出包含头文件中未定义实体定义的源文件名。 对于上面提到的例子:我需要找到包含函数“func()”定义的源文件。

已采取但未找到所需结果的方法: 我尝试使用“makedepend”和“gcc -MM &lt;source file names&gt;”来获取包含头文件中未定义实体定义的源文件,但它只是给出了一个规则,显示源文件直接或间接包含的头文件.

gcc -M test.cpp func.cpp 的输出如下:

test.o:test.cpp func.h
func.o:func.cpp func.h

我需要得到表单的输出:

test.o:test.cpp func.h func.cpp

谁能告诉我解决这个问题的方法。如何获取在包含的头文件中为我提供未定义实体定义的源文件名 提前致谢。

【问题讨论】:

  • 一种粗略(但可能有效)的方法是只使用grep
  • 在不相关的切线上,见stackoverflow.com/questions/5849457/…
  • @Ruchi:这里的用例是什么?您是否因为希望将其作为构建过程的一部分而担心性能?如果是这样,那为什么? (请注意,grep 非常快......)
  • 1.使用 grep 扫描 100 个文件并不需要太多时间。我从经验中知道。
  • @Ruchi:也许不是!但我仍然不清楚你为什么需要这样做?如果您想知道哪些函数未定义,只需运行完整的构建过程;链接器会抱怨缺少的任何东西。

标签: c++ c gcc


【解决方案1】:

我需要定义未定义实体的源文件

使用ctags 一次;例如:ctags test.cpp func.cpp func.hctags *.cpp *.h

然后找到func的定义源文件,执行e。 G。 grep '^func\&gt;' tags:

func func.cpp /^void func() {$/;" f

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2010-12-17
    相关资源
    最近更新 更多