-2

OK.

It's been a while since I wrote any C++.

And I'm rusty.

So what am I doing wrong here, and why?

#include <iostream>

std::string hello() {
  return "another green world";
}

int main(int argc, char **argv) {
  std::cout << hello() << std::endl;    
  return 0;
}

Then compiling with :

gcc test.cpp -o test

gives me

/tmp/ccxCCo47.o: In function `hello[abi:cxx11]()':
test.cpp:(.text+0x34): undefined reference to `std::allocator<char>::allocator()'
test.cpp:(.text+0x4b): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
test.cpp:(.text+0x57): undefined reference to `std::allocator<char>::~allocator()'
test.cpp:(.text+0x7b): undefined reference to `std::allocator<char>::~allocator()'
/tmp/ccxCCo47.o: In function `main':
test.cpp:(.text+0xcb): undefined reference to `std::cout'
test.cpp:(.text+0xd0): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
test.cpp:(.text+0xda): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cpp:(.text+0xe5): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
test.cpp:(.text+0xf1): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
test.cpp:(.text+0x116): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccxCCo47.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x156): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x16b): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccxCCo47.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
4
  • 1
    How do you compile your code? Commented Mar 18, 2019 at 3:56
  • updated question with it Commented Mar 18, 2019 at 4:02
  • 4
    Use g++ instead of gcc to compiler C++ code. g++ test.cpp -o test. Commented Mar 18, 2019 at 4:02
  • which compiler are you using ? because it seems to be working in ideone.com Commented Mar 18, 2019 at 4:03

1 Answer 1

1

You need to include your header file.

#include <iostream>

Also, try compiling with g++ instead of gcc.

g++ test.cpp
Sign up to request clarification or add additional context in comments.

5 Comments

I'm actually doing that. I just forgot to include it above.
Try compiling with g++? g++ test.cpp
what's the difference?
If using g++, libstdc++ will be linked by default. via stackoverflow.com/questions/3081815/c-errors-while-compiling
@interstar gcc is the C compiler, use g++ to compile C++. They're not the same language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.