I came across a curious bug today which I had somehow managed to avoid up until now.
file1.cpp:
#include <iostream>
inline void print() { std::cout << "Print1\n"; }
void y() { print(); }
file2.cpp:
#include <iostream>
inline void print() { std::cout << "Print2\n"; }
void x() { print(); }
main.cpp:
int x();
int y();
int main(){
x();
y();
}
Output:
Print1 (Expected Print2)
Print1
Because print() has inline linkage, this produces no multiple definition error (compiled with g++ -Wall file1.cpp file2.cpp main.cpp) and the duplicate symbol is silently collapsed. The actual case where I saw this was with inline class methods, not explicit inline functions, but the effect is the same.
I am wondering if there is a linker option or similar which will allow me to produce a warning when this type of error is made?
Print1 Print1. It should printPrint2as well. - Nawaz