2

I have 2 source files: test.cpp and func.cpp and 1 header file: 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()".

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.

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

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.

Ruchi
  • 31
  • 2
  • 3
    A crude (but probably effective) approach is to just use `grep`. – Oliver Charlesworth May 10 '15 at 12:29
  • On an unrelated tangent, see http://stackoverflow.com/questions/5849457/using-namespace-in-c-headers – Oliver Charlesworth May 10 '15 at 12:30
  • @JoachimPileborg We know that and I think the OP knows it as well. The root problem is the OP is trying to drive a nail with screw-driver. The problem trying to be solved (what source file fulfills this external dependency) and what -M provides as a solution to a different problem (what is each translation unit dependent on at compile-time) don't line up. – WhozCraig May 10 '15 at 12:35
  • http://scottmcpeak.com/autodepend/autodepend.html – William Pursell May 10 '15 at 12:37
  • @OliverCharlesworth: Thanks for your suggestion. But using grep would be time consuming, if in this test case directory, I have hundreds of source file each of about 30K LOC. – Ruchi May 10 '15 at 12:40
  • did u try `gcc -Wall test.cpp func.cpp -o test`? – CrApHeR May 10 '15 at 12:41
  • 3
    @Ruchi: What is the use-case here? Are you worried about performance because you want this as part of your build process? If so, then why? (And note that `grep` is pretty fast...) – Oliver Charlesworth May 10 '15 at 12:42
  • @JoachimPileborg: Yeah, I know output of "gcc -M" is correct, I just mentioned it, to give reference of strategy already used but no relevant result found. – Ruchi May 10 '15 at 12:43
  • @CrApHeR: yeah I tried "gcc -Wall " it didn't worked as well. – Ruchi May 10 '15 at 12:50
  • @OliverCharlesworth: No this is not a part of build process. I am worried about performance (time & memory) because in use case, I have 50 source files, which include 50 different header files and for each source file, scanning each included header file to find whether each entity is defined or not, if not then scanning all the other source files in current directory to find definition of such entity, would be tough. Please let me know, would you still suggestion use of "grep"? – Ruchi May 10 '15 at 13:00
  • 1
    1. Scanning a few 100 files with grep does not take that much time. I know from experience. – Ed Heal May 10 '15 at 13:02
  • 2
    @Ruchi: Perhaps not! But I'm still unclear as to why you need to do this? If you want to know which functions are undefined, just run your full build process; the linker will complain about anything that's missing. – Oliver Charlesworth May 10 '15 at 13:03
  • @OliverCharlesworth: I need the source file defining the undefined entities to resolve the linker error. Foe example for the above example specified: if I get to know that "func.cpp" contains definition of "func()" then I would include "func.cpp" in "test.cpp" instead of "func.h". This would resolve the linker error. I hope you get idea what the problem is. – Ruchi May 10 '15 at 13:17
  • @Ruchi: That's a little unclear. Surely this doesn't apply to all 50 header files. Rather, you just need to search, once, for each linker error that you receive. At which point, `grep` seems like the right solution. Alternatively, avoid the situation entirely by adhering to the convention that stuff declared in `foo.h` is defined in `foo.cpp`. That way, you know *a priori* which object files to link against; it's the same as the list of header files you've included. – Oliver Charlesworth May 10 '15 at 13:19

1 Answers1

0

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

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

func    func.cpp        /^void func() {$/;"     f
Armali
  • 18,255
  • 14
  • 57
  • 171