0

Consider 2 files: func.h:

#ifndef FUNC_H_
#define FUNC_H_
#include "func2.h"
#include "OrderedList.h"

class func{
    func2* pointer;
};


#endif /* FUNC_H_ */

and func2.h:

#ifndef FUNC2_H_
#define FUNC2_H_
#include "func.h"

class func2{
    func* joe;

};

#endif /* FUNC2_H_ */

I have a project containing this and an empty main.cpp file. It compiles fine, although in my opinion it shouldn't: when func.h is pasted onto func2.h, then the preproccesor recognizes func2_h is already defined and it stops there. Then func doesn't know who func2 is since it comes first.

However, if I include "func.h" in main then it doesn't compile as expected. I'm guessing that compilation ignores things that aren't used? Is this an optimization in eclipse (what I'm using) or something deeper?

1 Answers1

2

Yes, compilation ignores files which aren't included. Your compiler doesn't know anything about your project. All it knows about are the things which are passed to it on the command line, and typically, header files are not one of those things. If you take a look at the compilation command which Eclipse executes, you will most likely see main.cpp there, and no mention of your header files.

Read this question for a more thorough understanding of how C++ compilation works: How does the compilation/linking process work?

Community
  • 1
  • 1
Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274