93

Is it possible to specify extra header files to include from the command line (using GCC 4 / C++)?

Or is there any other way files can be included except with #include?

Background: I'm trying to compile a large code base on my own PC. The code is usually compiled in a cluster, with a complicated build system (SoftRelTools anybody?), which is intertwined with the operating system, such that it is virtually impossible to install it somewhere else (literally hundreds of makefiles and shell scripts, and hard coded paths to network drives). However, the actual code is fairly straightforward, and compiles fine, BUT it is missing a lot of includes (mostly a la "include <vector>" and "include <math.h>"). I'm guessing the build system takes care of this usually, but I have to go through the code and add the includes manually, which I'd rather avoid.

jww
  • 97,681
  • 90
  • 411
  • 885
jdm
  • 9,470
  • 12
  • 58
  • 110

3 Answers3

105

I found the -include option. Does this what you want?

-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.

ejohnso49
  • 1,336
  • 1
  • 15
  • 20
foraidt
  • 5,519
  • 5
  • 52
  • 80
  • 11
    FYI the -include feature is primarily intended to support precompiled headers. This way you can inject your .pch file into all your source files without having to edit every single one. – gavinb Aug 02 '10 at 12:09
  • 4
    Thanks! Although I did RTFM (M=manpage), I missed that. I also thought PCHs were just a Visual C++ feature... but it seems like this is what they were using in the build system... – jdm Aug 02 '10 at 12:48
  • 4
    the corresponding command line argument for Visual C* would be `/FI`, just in case anyone needs to port builds that use this. – PypeBros Nov 20 '19 at 15:32
  • anything similar for #include (angle brackets)? to be more detailed i'm interested in clang support for it – 4ntoine Aug 11 '20 at 19:39
  • Fantastic. @gavinb I used this for a baremetal use-case when I needed to supplement some of the libc headers with a few substituted functions and/or extensions to my libc code. All my code can still use standard headers for prototypes, but also forcefully include some extra definitions I need to make them work on my platform. – sherrellbc Oct 07 '20 at 10:40
  • @4ntoine What do you mean? That's exactly what this option is for. The `#include` directive instructs the preprocessor to copy the contents of that file into an intermediate file that is then parsed by the compiler. This options effectively acts as if you have added `#include ` to the top of the source being compiled. – sherrellbc Oct 07 '20 at 10:42
  • @sherellbc i mean that there is a difference between `#include ""` and `#include <>` in terms of search paths. – 4ntoine Oct 07 '20 at 14:48
35

From the gcc manual:

-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.

Bart
  • 2,062
  • 15
  • 19
YuppieNetworking
  • 8,672
  • 7
  • 44
  • 65
15

According to gcc documentation, the command line switch "-include file" would do the job.

tibur
  • 11,531
  • 2
  • 37
  • 39