开发者

Should standards be specified in source code or in CPPFLAGS?

开发者 https://www.devze.com 2023-02-04 05:50 出处:网络
Is it better to #define _BSD_SOURCE or to set CPPFLAGS=-D_BSD_SOURCE? It seems to me that if a piece of source 开发者_JS百科code relies on a particular standard, it is best to spell it out explicitly

Is it better to #define _BSD_SOURCE or to set CPPFLAGS=-D_BSD_SOURCE?

It seems to me that if a piece of source 开发者_JS百科code relies on a particular standard, it is best to spell it out explicitly in the code itself with a #define. However, a lot of commentary suggests that specifying the standard on the compile line is more appropriate. What are the advantages of omitting the standard from the source code and only specifying it at compile time?


If you specify defines in your source there is a risk that the same header file can be included in several source files (translation units) but with different preprocessor definitions, which can lead to One Definition Rule violation which is often a pain to debug.

By specifying defines for the whole project rather than in the individual source files the chance of such One Definition Rule violation is minimized.

Also, if a need arises to add a new define, you change only one makefile, rather than all the source files.

0

精彩评论

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