0

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.

Community
  • 1
  • 1
mintaka
  • 982
  • 1
  • 9
  • 25

1 Answers1

0

Precompiled headers are an efficiency measure. The code should still be valid if the compiler would compile them using the normal methods. In particular, your Visual C++ code should compile on Eclipse and vice versa.

I strongly suspect your problem is on the Eclipse side. Why is a.cpp virtually empty? It should contain the definition for all declaractions in a.h.

If you're including "precomp.h", it probably should come before the contents of "a.h". There are essentially two ways of achieving that. One is to include both headers in that order (Visual Studio style) and the other is to include "precomp.h" before any declaration in "a.h". (MinGW style)

The rules for precompilation are somewhat more relaxed in MinGW as it pre-compiles less. That

MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Sorry, I didn't mean there is only included header files in `a.cpp`. The definitions are there in `a.cpp`. So the problem is not due to that. Any other explanations? Thanks! – mintaka Aug 27 '12 at 07:29
  • @Giantron: Sure, but you could best experiment with a very simple setup so that you can show all code if it still fails. Two headers on <10 lines should be sufficient. – MSalters Aug 27 '12 at 08:08
  • I found the problem.. Sorry I didn't pay much attention to properly place `using namespace ...;` The problem was due to the misplacement of that. May you advise where is the best to put `using namespace ...;`? Should I always put it inside header files or source files? Thanks a lot. – mintaka Aug 27 '12 at 09:05
  • @Giantron: That's easy. ALWAYS in source files. – MSalters Aug 27 '12 at 09:06