views:

34

answers:

2

I've run into trouble in the past when I've tried porting some C++ code written on Mac OS X to a Linux system, or trying to compile code written against an older version of gcc/g++ with a newer one:

It seems that some (older?) versions of gcc/g++ would automatically include some header files for you.

For example, code that uses printf should require #include <stdio.h>. And code that uses memcpy should require #include <string.h>. But depending on the version of gcc I'm using, it will occasionally include these for me.

It wreaks havoc when I forget to include something and then never get errors until I go to compile the code on another system. At that point it's a game of running all over the project and fixing the includes.

Has anyone else run into this? Is there a way to force gcc to autoinclude or to not autoinclude? Or, is there a way to know what it's autoincluding?

A: 

When compiling on different systems, you might meet different problems and not only includes. I would suggest investing in a continuous build system that will compile on all OS you need after each update of the code, so you are rapidly aware of any portability issue.

You can also put all common system header files inside a specific header file you will write and systematically include it in all your files.

Benoit Thiery
This is a good suggestion, but in one case, it was a third party library (an old version of OpenSceneGraph) and some old code that broke when upgrading to a newer version of gcc. Auto-compiling for multiple OSes wouldn't have helped here, and I suspect that maybe the problem was just that #includes inside the system include files were reorganized between gcc releases :/
Dave
+1  A: 

Are you sure it's not other headers pulling those one's in, and on the other platforms not doing so?

Preet Sangha