Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should standards be specified in source code or in CPPFLAGS?

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

It seems to me that if a piece of source 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?

like image 555
William Pursell Avatar asked Jan 13 '11 14:01

William Pursell


1 Answers

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.

like image 136
Maxim Egorushkin Avatar answered Oct 05 '22 14:10

Maxim Egorushkin