获取包含头文件中声明的实体定义的源文件名?

时间:2022-03-03 15:07:02

I have 2 source files: test.cpp and func.cpp and 1 header file: func.h

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

Contents of these files are as follows:

这些文件的内容如下:

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 */

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

Problem: Problem is to find out the source file name which contains definition of undefined entities in header files. For the example mentioned above: I need to find out source file which contains definition of function "func()".

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

Approaches taken but no required result found: I tried using "makedepend" and "gcc -MM <source file names>" to get the source files which contains definition of undefined entities in header files, but it just gave a rule, showing the header files which are included directly or indirectly by the source files.

找到方法但没有找到所需的结果:我尝试使用“makedepend”和“gcc -MM <源文件名> ”来获取源文件,其中包含头文件中未定义实体的定义,但它只是给出了一个规则,显示了标题源文件直接或间接包含的文件。

Output of gcc -M test.cpp func.cpp is as follows:

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

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

I need to get an output of the form:

我需要获得表单的输出:

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

Can anyone please, let me know the way of solving this problem. How to get the source file name which provides me definition of undefined entities in included header file Thanks in advance.

有谁可以请,让我知道解决这个问题的方法。如何获取源文件名,它提供了包含头文件中未定义实体的定义,提前感谢。

1 个解决方案

#1


I need the source file defining the undefined entities

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

Use ctags once; for example: ctags test.cpp func.cpp func.h or ctags *.cpp *.h

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

Then to find out the defining source file of func, do e. g. grep '^func\>' tags:

然后找出func的定义源文件,做e。 G。 grep'^ func \>'标签:

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

#1


I need the source file defining the undefined entities

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

Use ctags once; for example: ctags test.cpp func.cpp func.h or ctags *.cpp *.h

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

Then to find out the defining source file of func, do e. g. grep '^func\>' tags:

然后找出func的定义源文件,做e。 G。 grep'^ func \>'标签:

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