Is there a way to automatically detect such problems using GCC?
Only the linker can detect if you are linking incompatible code, not the compiler.
An alternative gold linker may detect some problems with the --detect-odr-violations option.
Alternatively, how do other people avoid this problem? Build a verified version of the library with a different name or something like that?
I just guarantee that I will rebuild everything when I want to use debug mode, I do not think that I ever wanted to save a library around it that was built in debug mode. This is for debugging, not for normal use.
In any case, I rarely use -D_GLIBCXX_DEBUG , I often do something like:
#if 0 # include <debug/vector> namespace my_class_stl = __gnu_debug; #else #include <vector> namespace my_class_stl = std; #endif struct my_class { typedef my_class_stl::vector<int> container; typedef container::iterator iterator; // ... };
Then I change the preprocessor condition when I want to use the debug mode vector for this particular class without affecting each container in the program. Since the change includes writing to the file (and therefore updating its timestamp), everything that depends on this header will be restored using make , and there are two different types: std::vector<int> and __gnu_debug::vector<int> , which have different characters and may not be confused with a linker.
A simple definition of _GLIBCXX_DEBUG does not rebuild all the dependencies and silently changes the definition of std::vector globally, instead of changing specific containers to another type with a different name __gnu_debug::vector
Jonathan wakely
source share