0

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?

Aaron Franke
  • 3,268
  • 4
  • 31
  • 51
  • 7
    To compile (and link) c++ code use `g++ main.cpp` instead of `gcc main.cpp`. – πάντα ῥεῖ Mar 10 '19 at 07:32
  • Thanks, that worked. If you post an answer I'll accept it. Too bad I've been downvoted for asking what I thought was a legitimate question. – Aaron Franke Mar 10 '19 at 07:34
  • 1
    You aren't downvoted, your question is. It's a simple mistake invoking the compiler, that is asked here fairly regularly. So people are within their right to consider it not useful (something the downvote button indicates is a reason to vote, if you care to look). – StoryTeller - Unslander Monica Mar 10 '19 at 07:40
  • @AaronFranke There>'s probably even a duplicate beyond our [_one catches it all meta duplicate_](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) for linker errors. I am pretty sure that's mentioned there also though. – πάντα ῥεῖ Mar 10 '19 at 07:44
  • Also, why doesn't `gcc` show any meaningful warnings when compiling a file with the extension `.cpp`? Seems like a no-brainer to add, especially if this mistake is made often enough to be asked regularly on SO. – Aaron Franke Mar 10 '19 at 07:45
  • 1
    @AaronFranke `gcc` accepts compiling c++ code, but has different defaults for the standard libraries to link with. That behavior is intended and sometimes quire useful. – πάντα ῥεῖ Mar 10 '19 at 07:46
  • If that's the case, then switching to `g++` is not the only solution to this problem and therefore I'd argue the question has merit. – Aaron Franke Mar 10 '19 at 07:47
  • @AaronFranke Have an upvote from me for 'the old fashioned way' but the others are right, this question is often asked and therefore not useful. – john Mar 10 '19 at 07:50
  • There is no duplicate because using `gcc` instead of `g++` rightly fall under the "typographical or can no longer be reproduced" category of close reasons. And so are removed by the roomba script. – StoryTeller - Unslander Monica Mar 10 '19 at 07:53
  • @AaronFranke _"then switching to g++ is not the only solution to this problem"_ Sure, you could specify the missing c++ standard libraries explicitely. – πάντα ῥεῖ Mar 10 '19 at 07:54
  • If a question is often asked, wouldn't that make it *more* useful? – Aaron Franke Mar 10 '19 at 07:55
  • 1
    Not really. The mistake is a mistake, and to explain the difference in linking process between the two invocations, we have this https://stackoverflow.com/questions/6735277/any-difference-in-linking-with-gcc-vs-g – StoryTeller - Unslander Monica Mar 10 '19 at 07:56

0 Answers0