Is there a way for GCC to produce a warning while linking libraries that contain classes with the same name? For example
Port.h
class Port {
public:
std::string me();
};
Port.cpp
#include "Port.h"
std::string Port::me() { return "Port"; }
FakePort.h
class Port {
public:
std::string me();
};
FakePort.cpp
#include "FakePort.h"
std::string Port::me() { return "FakePort"; }
main.cpp
#include "Port.h"
int main() {
Port port;
std::cout << "Hello world from " << port.me() << std::endl;
return 0;
}
Building
# g++ -c -o Port.o Port.cpp
# ar rc Port.a Port.o
# g++ -c -o FakePort.o FakePort.cpp
# ar rc FakePort.a FakePort.o
# g++ -c -o main.o main.cpp
# g++ main.o Port.a FakePort.a
# ./a.out
Hello world from Port
Change library order
# g++ main.o FakePort.a Port.a
# ./a.out
Hello world from FakePort
According to this page:
If a symbol is defined in two different libraries gcc will use the first one it finds and ignore the second one unless the second one is included in an object file which gets included for some other reason.
So the above behavior make sense. Unfortunately I am inheriting a sizable code base that doesn't make use of namespaces (and adding them isn't feasible right now) and uses some generic class names throughout multiple libraries. I would like to automatically detect duplicate names at link time to make sure that the wrong copy of a class isn't accidentally being instantiating. Something like:
# g++ -Wl,--warnLibraryDupSymbols main.o FakePort.a Port.a
Warning: Duplicate symbol: Port
but I can't find anything in the GCC linker options to do that. Is it possible to get GCC to automatically detect and report such cases?