开发者

fail compile if required flags aren't present

开发者 https://www.devze.com 2023-04-06 14:42 出处:网络
I have some legacy code that needs certain gcc flags passed in.Can I add pre-processor checks for these flags?

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-stri开发者_高级运维ct-aliasing is required!
#endif


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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消