Search code examples
cgccincludepreprocessor

gcc include header and get output after preprocessing


I want the preprocessed output of a .c file, but I also want to include a header file without the macro "include..." in the .c file. Usually, you add the -I option for including a directory where headers are. But if I want to combine -I and -E, gcc does't seem to include my header files in the specified directory. My command:

gcc -E -I/externDefines myFirmware.c > myFirmware.preprocessed

Does anyone know what the problem could be?


Solution

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