Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use gcc preprocessor to define filename for #include

I would like to specify the name of a C include file at compile time as a C flag.

e.g.

#include MY_INCLUDE_FILE
int main(int argc, const char * argv[]) {...}

Would be expaned by the pre-processor to

#include "some_incfile.h"
int main(int argc, const char * argv[]) {...}

Doing something like this

gcc -DMY_INCLUDE_FILE="some_incfile.h" main.c

I have attempted using the stringizing operator # to expand but have only gotten errors such as error: expected "FILENAME" or <FILENAME>

Is this even possible? -D define is not entirely necessary, the important part is that the include filename can be set from the gcc command line

like image 362
bits Avatar asked Sep 06 '25 03:09

bits


1 Answers

You have to escape the ":

gcc -DMY_INCLUDE_FILE=\"some_incfile.h\" main.c
like image 89
koalo Avatar answered Sep 07 '25 21:09

koalo