I'm currently using both Visual Sstudio 2010 and Eclipse+MinGW for my C++ project. I've experienced different compiler behaviors, which needs your kind explanation. Here are one example:
Suppose I have a.h
and a.cpp
. And I have pre-compiled header file (i.e., stdafx.h
in VS, and precomp.h
in Eclipse (I created one for Eclipse)). I'm gonna include some basic header files in the pre-compiled header file (e.g., core.hpp in opencv). And in both a.h
and a.cpp
, I will use some classes (e.g., Mat in opencv) declared in the basic header files which are included in the pre-compiled header.
For Visual Studio 2010, I have to put stdafx.h in every .cpp file (as in the link). So in a.cpp
, I have this in the begining:
#include "stdafx.h"
#include "a.h"
Besides, I don't include stdafx.h
in a.h
. I was expecting the error that the compiler cannot find the class declared in the basic header files, but it turns out the compilation is just fine under VS. There are no errors at all.
For Eclipse+MinGW, I do like this: in a.h
, I put #include "precomp.h"
at the begining. And in a.cpp, I only put #include "a.h"
(As told by some friends, this is the C++ standard way to include header files). After compilation, this is no error, which is of no doubt. But if I do it similarly as what I did for Visual Studio, i.e., I don't include precomp.h
in a.h
. There will be errors, indicating some classes are not declared in this scope. As concerned by @MSalters, I should **highlight** that both a.h and a.cpp contain proper declarations and definitions.
So anyone can help explain the difference bahaviors of VS and Eclipse+MinGW to me? Many thanks.