Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include header files using command line option?

Tags:

c++

include

gcc

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.

like image 234
jdm Avatar asked Aug 02 '10 11:08

jdm


People also ask

How do I include a header file?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.

HOW include header file in C Linux?

To import a header, use the #include, a preprocessor directive telling the compiler that it should import and process the code before compiling the rest of the code. On a typical C program, it should contain the stdio. h header file, which is the standard header file for input and output streams.

Can you include header files in header files?

Yes, this will work. Note, however, that if you include a lot of headers in this file and don't need all of them in each of your source files, it will likely increase your compilation time.

How do I add files to GCC?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets. For example, if /usr/include/sys/stat. h contains #include "types.


2 Answers

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.

like image 106
foraidt Avatar answered Nov 16 '22 04:11

foraidt


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.

like image 27
YuppieNetworking Avatar answered Nov 16 '22 03:11

YuppieNetworking