Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fail compile if required flags aren't present

Tags:

c++

c

gcc

I have some legacy code that needs certain gcc flags passed in. Can I add pre-processor checks for these flags?

For example, let's say I need -fno-strict-aliasing, can I do something like this:

#ifndef _FNO_STRICT_ALIASING
   #error -fno-strict-aliasing is required!
#endif
like image 295
paleozogt Avatar asked Sep 22 '11 16:09

paleozogt


2 Answers

You can use

#pragma GCC optimize "no-strict-aliasing"

to compile the file with that flag (overriding what was specified on the command line). You can also use

__attribute__((optimize("no-strict-aliasing")))

to apply the flag to a single function within a source file...

like image 115
Chris Dodd Avatar answered Nov 12 '22 17:11

Chris Dodd


There is definitely no #define for it, at least on my version of GCC.

To see all predefined preprocessor symbols:

g++ -dM -E - < /dev/null

I do not think there is any way to test these options. However, if you are using GCC 4.4 or later, you can use the "optimize" function attribute or the "optimize" #pragma to enable specific options on a per-function or per-file basis.

For example, if you add this to a common header file:

#if defined(__GNUC__)
#pragma GCC optimize ("no-strict-aliasing")
#else
#error "You are not using GCC"
#endif

...it should enable the option for every file that includes the header.

[update]

OK so it took me about 10 minutes too long to compose this answer. I am going to leave it here anyway for the links to the GCC docs.

like image 31
Nemo Avatar answered Nov 12 '22 16:11

Nemo