Is there software/tool to see the order in which header files are being included after compilation of a C++ application? I find myself often running into circular dependency issues and seeing the "undefined reference" error message :(
UPDATE #1
I am familiar with header include guards and I am using them, but for some reason I'm still having an issue with circular dependency.
In my case, I have many situations where a class A uses class B, and vice versa. In those cases, I use forward declaration for example "class B;" at the top of A.h and vice versa.
Should it ever be the case where I'll need to do "#include "B.h"" at the top of A.h? Or is "class B;" sufficient on its' own at the top of A.h?
UPDATE #2
Hello, below is a snippet of the code that I am having trouble compiling/linking. There are 3 classes below:
A.cpp
#include "A.h"
namespace sef
{
A::A() {
// TODO Auto-generated constructor stub
b = 0;
}
A::~A() {
// TODO Auto-generated destructor stub
}
bool A::execute()
{
C::connectAndSaveFile();
b->start();
return true;
}
}
A.h
#ifndef A_H_
#define A_H_
//#include "B.h"
class B;
#include "C.h"
namespace sef {
class A {
public:
B* b;
bool execute();
A();
virtual ~A();
};
}
#endif /* A_H_ */
B.cpp
#include "B.h"
B::B() {
// TODO Auto-generated constructor stub
engine = 0;
bool result = engine->execute();
cout << result << endl;
}
B::~B() {
// TODO Auto-generated destructor stub
}
void B::start()
{
cout << "B::start()" << endl;
}
B.h
#ifndef B_H_
#define B_H_
#include <iostream>
#include <string>
using namespace std;
//#include "A.h"
namespace sef {
class A;
}
class B
{
public:
sef::A* engine;
B();
virtual ~B();
void start();
};
#endif /* B_H_ */
C.cpp
#include "C.h"
C::C() {
// TODO Auto-generated constructor stub
}
C::~C() {
// TODO Auto-generated destructor stub
}
void C::connectAndSaveFile()
{
cout << "C::connectAndSaveFile()" << endl;
}
C.h
#ifndef C_H_
#define C_H_
#include <iostream>
#include <string>
using namespace std;
class C {
public:
C();
virtual ~C();
static void connectAndSaveFile();
};
#endif /* C_H_ */
I seem to be getting the error:
***g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\A.o" "..\\src\\A.cpp"
..\src\A.cpp: In member function 'bool sef::A::execute()':
..\src\A.cpp:23:4: error: invalid use of incomplete type 'class B'
In file included from ..\src\A.cpp:7:0:
..\src\A.h:12:7: error: forward declaration of 'class B'***