Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I control the way gcc/g++ automatically includes headers?

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?

like image 558
Dave Ceddia Avatar asked Oct 15 '10 12:10

Dave Ceddia


2 Answers

-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

like image 108
sinojelly Avatar answered Nov 03 '22 21:11

sinojelly


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

like image 28
Preet Sangha Avatar answered Nov 03 '22 22:11

Preet Sangha