According to this post the Google C++ Testing Framework considers “make install” a bad practice.
http://groups.google.com/group/googletestframework/browse_thread/thread/668eff1cebf5309d
The reason for this is that this library violates the “One Definition Rule”.
http://en.wikipedia.org/wiki/One_Definition_Rule
Somewhere further in the thread it says: “If you pass different -DGTEST_HAS_FOO=1 flags to different translation units, you’ll violate the ODR. Or sometimes people use -D
to select which malloc library to use (debug vs release), and you have
to use the same malloc library across the board.”
My questions:
- What is it exactly this project is doing wrong?
- What can we learn from this? How can we write code more defensive to prevent violating the ODR?
The straight answer to the question would be: do not write code that depends on compiler parameters. In this case, the whole discussion stems from the fact that the code is different based on compiler flags (most probably by means of
#ifdefor other preprocessor directives). That in turn means that while the codebase is the same, by changing compiler flags the processed code will be different, and a binary compiled with one set of flags will not be compatible with a binary compiled with a different set of flags.Depending on the actual project, it might be impossible to decouple the code from compiler flags, and you will have to live with it, but I would recommend avoiding in as much as possible code that can be configured from the compiler command line, in the same way that you should avoid DEBUG only code with side effects. And when you cannot, document the effect that different compiler flags have.