DanielHsH
DanielHsH

Reputation: 4453

Is it possible (How) to give compiler directives inside c/c++ code

I would like my source code to give specific compiler directives (like remove symbols, speed optimization, stack frame sizes etc).

Just like #pragma comment(lib, "Mylibrary.lib") gives a command to linker to include a given library I wonder if I can do something like

#if (__APPLE__)||(__ANDROID__)
/I"..\mobile\headers"
 -fvisibility=hidden -s 
#elif (WIN32)
 -O3
#endif

Meaning (activate -O3 optimization for windows32, while for mobile phones hide some symbols and include specific headers).

I can do it using Make Files or Visual studio projects but I wonder if it can be done within the source code to make it more platform and IDE independent

P.s. I understand that directives need to be matched to each specific compiler (gcc, LLVM, microsoft,...) and platform

Upvotes: 0

Views: 285

Answers (1)

utnapistim
utnapistim

Reputation: 27375

Not really. You can (to a degree) add pragma comments that will influence a compiler (for example, the pragma lib you showed).

Such a solution would be limited by compiler-specific support for pragmas.

Keeping such instructions in header files is a bad decision, because having to edit a code file, to change the build system settings is bad for maintenance.

If you split settings in a single file for multiple compilers, you will also end up with a situation where you could change things for one compiler, and break compilation settings for the others.

Keeping build settings in header files will also spread your compilation settings to places other than your build system configuration (e.g. Visual Studio property sheets or cmake files) and make everything harder to track, document, follow, and ultimately maintain for the duration of your project.

Long term, you are probably much better off, cleaning your official build system, than adding hacks to the build, in include files.

Upvotes: 1

Related Questions