I'm trying to learn some C++ "the old fashioned way" with GCC and text files. I've hit this problem:
#include <string>
int main(int arcg, char* argv[])
{
std::string test = "test";
}
When I run gcc main.cpp
I get this error:
/tmp/ccQ34IRS.o: In function `main':
main.cpp:(.text+0x27): undefined reference to `std::allocator<char>::allocator()'
main.cpp:(.text+0x3e): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
main.cpp:(.text+0x4a): undefined reference to `std::allocator<char>::~allocator()'
main.cpp:(.text+0x56): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
main.cpp:(.text+0x7b): undefined reference to `std::allocator<char>::~allocator()'
/tmp/ccQ34IRS.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
If I remove the std::string
line, such that main is empty, GCC compiles it fine. Searching the error led me to this SO question, but adding #define _GLIBCXX_USE_CXX11_ABI 0
did not help.
$ gcc --version
gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
EDIT: Ok, so you're supposed to use g++
for compiling C++ code, but gcc
can also do it? Doesn't that mean that this question has multiple solutions worth discussing and shouldn't be closed? And if this is really asked often, why is there no open duplicate that can be linked when it's asked?