Home > Software engineering >  Passing custom compiler flags to bsds gcc
Passing custom compiler flags to bsds gcc

Time:07-11

So, depending on how I decide to compile a program, I want to be able to execute a set of functions. This could normally be done with just a few variables and comparisons, but since I will be distributing it to systems that only have the ELF, it needs to be known at compile time what to run. Is it possible to pass in a custom gcc flag, say -flagset that makes it then set a MACRO in my code if that flag is set? I seen How to specify custom compiler flags for Visual Studio Compiler but that is a bit vague and not appropriate for my needs

CodePudding user response:

From the gcc manual:

3.13 Options Controlling the Preprocessor

-D name Predefine name as a macro, with definition 1.

-D name=definition The contents of definition are tokenized and processed as if they appeared during translation phase three in a ‘#define’ directive.

That is, you can set any macro value via the -D option and that will be seen by the code. Example:

gcc -DSOME_FLAG test.c

Then in the code it can be checked as such:

#ifdef SOME_FLAG
    /* do code for SOME_FLAG enabled case */
#endif
  •  Tags:  
  • c gcc
  • Related