Warn if gcc is used as CXX_COMPILER?
When gcc
is used as CXX_COMPILER, the standard library is not automatically included:
/usr/bin/ld: /tmp/ccJGZzAq.o: in function `main':
helloworld.cpp:(.text+0xe): undefined reference to `std::cout'
/usr/bin/ld: helloworld.cpp:(.text+0x13): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: /tmp/ccJGZzAq.o: in function `__static_initialization_and_destruction_0(int, int)':
helloworld.cpp:(.text+0x43): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: helloworld.cpp:(.text+0x58): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
This could be fixed by adding -lstdc++
, but my understanding is that g++
is the correct binary to be called for C++ files. A short web search seems to confirm this: https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc
For projects that can be built with different compilers, changing the compiler on the command line (cmake -DCMAKE_CXX_COMPILER=xxx ..
) is common. I have seen gcc being used both by accident or because the user did not know any better. While one might easily argue that this is a user error, a simple warning would keep many hairs from going gray. Same for clang.
I understand that this would add warnings to projects that work perfectly fine (because they do not rely on -lstdc++
and others being set). Not sure if a policy is the right way to handle this.