I use GoogleTest for testing my C++ projects, and after finding that precompiled libraries were no longer distributed in the Ubuntu package, I found the following on the project website:
If you compile Google Test and your test code using different compiler
flags, they may see different definitions of the same
class/function/variable (e.g. due to the use of #if in Google Test).
Therefore, for your sanity, we recommend to avoid installing
pre-compiled Google Test libraries. Instead, each project should
compile Google Test itself such that it can be sure that the same
flags are used for both Google Test and the tests.
What I take from this is that it’s a bad idea to compile GoogleTest separately from the project being testing. What I don’t understand is whether this is just a GoogleTest thing, or if this is a general thing for linking libraries.
Question
Is there any situation in which it is unsafe to link to precompiled third-party libraries, compiler flags or otherwise, and if not, what’s so special about GoogleTest?
There are some compiler flags, notably those that work with alignment, that could cause a problem.
From GCC i386 and x86-64 flags
For example, using that flag on a 32-bit system will for doubles and long longs to be 64-bit aligned. If you compile a library without the flag, then attempt to use the library while using the flag, structures that contain the types above may have different alignments, and may not interoperate.
Other (much simpler) cases can also be ensuring the same set of #defines to ensure the same function/structure/class definitions are used (and other such ODR violations). For example, the use of ‘–std=c++11’ in gcc, which enables the C++ 11 versions of the Standard Library classes, which in some cases are different than the previous versions.