How Do You Troubleshoot a C++ Program That Will Not Compile Due to Undefined Reference Errors?

Problem scenario: You try to compile a C++ program with gcc, but you get this output:

/tmp/ccj6eKDl.o: In function `main':
q.cpp:(.text+0x14): undefined reference to `std::cout'
q.cpp:(.text+0x19): 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*)'
q.cpp:(.text+0x28): undefined reference to `std::cout'
q.cpp:(.text+0x2d): 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*)'
/tmp/ccj6eKDl.o: In function `__static_initialization_and_destruction_0(int, int)':
q.cpp:(.text+0x55): undefined reference to `std::ios_base::Init::Init()'
q.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

What do you do?

Root cause:  gcc will not link the standard C++ libraries.

Solution

Use g++ instead of gcc.

Leave a comment

Your email address will not be published. Required fields are marked *