Include Header Files Using Command Line Option

Include header files using command line option?

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.

How To Include Header Files In C

You need to add myhead.c file to gcc arguments gcc processSimulator.c my head.c

gcc include header and get output after preprocessing

-I does not mean “Include the header files from the given directory in the compilation.” It means “When searching for a file requested with #include, look for the file in the given directory.”

GCC has a command-line switch, -include file that will include a file in the compilation. However, it includes a single file, so you must list each file you want included; it will not automatically include all header files in a single directory. The command-line shell you are using may have features that help generate a list of -include switches with the file names.

A portable way to include a header file X.h while compiling Y.c without changing Y.c would be to create an auxiliary file containing:

#include "X.h"
#include "Y.c"

and then compile that instead of Y.c.

How to include header files in GCC search path?

Try gcc -c -I/home/me/development/skia sample.c.

Add header to every file in project setting (gcc -include)

You can change the CDT GCC Built-in Compiler Settings on a project-by-project basis.

  • Go to Project -> Properties -> C/C++ General -> Preprocessor Include Paths, Macros etc.
  • Select the Providers tab.
  • Select CDT GCC Built-in Compiler Settings
  • Unselect Use global provider shared between projects
  • Add the required GCC flags to the Command to get compiler specs: input box

(see highlighted text in image below)

Sample Image

You probably want to click Store entries in project settings folder

cmake include header into every source file

CMake doesn't have a feature for this specific use case, but as you've hinted, compilers such as GCC have the -include flag which acts as if there was an #include "foo.h" in the source file, and since CMake can pass arguments to compilers, you can do it via add_definitions.

This answer covers what the flag is for GCC, Clang and MSVC which should cover a lot of bases. So in CMake, detect what the compiler is and pass the appropriate flag.

Here's what the CMake code might look like:

if(MSVC)
add_definitions(/FI"foo.h")
else()
# GCC or Clang
add_definitions(-include foo.h)
endif()

Comments

In general, doing this is a bad idea. Code inspection tools (like IDEs, or doxygen) will be confused by it, not to mention other humans looking at the code. If not all source files actually require the definition, adding extra #includes will slow down compile time. If you actually do need the same header (and it's not a system header) in all your source files, it may be symptomatic of high coupling in your code. And for what benefit? Not having to add one line to your files?

However, it's necessary to note that compilers support this for a reason; there are a few weird edge cases (example 1, example 2) where it's a useful thing to do.

Just be aware that you're doing this for the right reasons.

Include one header file in each source file

You can use the -include flag for clang or GCC. From the man page:

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

Example:

clang -include header.h -c file1.c
clang -include header.h -c file2.c
clang -include header.h -c file3.c
clang -o app file1.o file2.o file3.o

MSVC has the /FI flag, which is similar.

Is there a way to include a header in every compilation unit without modifying every source file?

From man gcc:

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



Related Topics



Leave a reply



Submit