10

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?

9
  • May be if gcc does it automatically it would become too subtle for it to understand same methods names in different libraries. But if there is some automatic solution I would like to know. Commented Oct 15, 2010 at 12:22
  • 4
    Are you sure it's not other headers pulling those one's in, and on the other platforms not doing so? Commented Oct 15, 2010 at 12:23
  • 2
    gcc doesn't "auto include" - you're probably just getting indirect includes via headers that you have explicitly #included, as Preet suggest above. Bottom line: you need to fix your code. Commented Oct 15, 2010 at 12:29
  • @Preet, thanks for that suggestion. I bet that's what is happening. If you want to make that suggestion as an answer I'll check it off. Commented Oct 15, 2010 at 13:08
  • @PaulR: No, gcc does "auto include" stuff. On several computers I have access to, int main(){printf("Hello world\n");} compiles fine without any includes. I've tested both using gcc 4.6.3, gcc 4.9.2 and clang 3.5.0, both on linux. But on a mac it doesn't work. So this is clearly system dependent, and I wish it were documented somewhere. Too bad the poster here ended up getting the wrong answers. Commented Nov 18, 2014 at 0:59

3 Answers 3

5

-include file Process file as if #include "file" appeared as the first line of the primary source file. However, the first directory searched for file is the preprocessor's working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the #include "..." search chain as normal. If multiple -include options are given, the files are included in the order they appear on the command line.

http://gcc.gnu.org/onlinedocs/gcc/Preprocessor-Options.html

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

0

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.

1 Comment

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 :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.