The GCC compiler suite contains different commands for compiling C and C++ code. If you use the wrong one perhaps you would expect the compiler to give an error like this:
This is not a C file.
But as you found out the hard way, this is not the case.¨
gcc
is the compiler for C code and g++
is the compiler for C++ code. And if you try to compile C++ using gcc
you get an error like this:
$ gcc hello.cpp
/tmp/cc9uZOKP.o: In function `main':
hello.cpp:(.text+0xe): undefined reference to `std::cout'
hello.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*)'
hello.cpp:(.text+0x20): 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)'
/tmp/cc9uZOKP.o: In function `__static_initialization_and_destruction_0(int, int)':
hello.cpp:(.text+0x50): undefined reference to `std::ios_base::Init::Init()'
hello.cpp:(.text+0x65): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status