OneArb
OneArb

Reputation: 473

Conditional code depending on a gcc switch

I'd like to cross-compile specific 32-bit and 64-bit code using an #if directive based on gcc switches.

Are there any macros set by GCC I could use instead of setting a macro using the -D switch?

Can I somehow pick up the switches used in the gcc command options to test them at the preprocessor stage?

So far I found out that -m64 defines on my 64-bit machine

   __x86_64__

whereas -m32 undefines

   __i386__ __i486__ __i586__ __i686__

Upvotes: 0

Views: 212

Answers (1)

Giacomo Catenazzi
Giacomo Catenazzi

Reputation: 9533

You could use INT_MAX value from <limits.h>. This is C, so independent to compiler.

You can also check https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html to have some other predefined macros based on compiler options.

In general in recent time, it is recommend to write code portable, also with if that tests conditions that true only on specific architectures. Optimizer will remove unneeded branches.

Upvotes: 2

Related Questions