C++ Linker Error on M1 Mac running Hello World Program

I'm trying to run the program below on my M1 Pro MacBook Pro from the command line, but I am getting linker errors. Also, I do not want to use XCode, I like to develop in vim in my terminal and want to develop in the terminal rather than XCode.

#include <iostream>
#include <string>

inline void print_string(std::string s = "Hello World!")
{ std::cout << s << std::endl; }

int main() {
    print_string();
}

The above program generates these errors:

Undefined symbols for architecture arm64:
  "std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
      std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&) in hello_world-fcbd23.o
  "std::__1::ios_base::getloc() const", referenced from:
      std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in hello_world-fcbd23.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(char const*, unsigned long)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string<std::__1::nullptr_t>(char const*) in hello_world-fcbd23.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(unsigned long, char) in hello_world-fcbd23.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
      _main in hello_world-fcbd23.o
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in hello_world-fcbd23.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in hello_world-fcbd23.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in hello_world-fcbd23.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in hello_world-fcbd23.o
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in hello_world-fcbd23.o
  "std::__1::cout", referenced from:
      print_string(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) in hello_world-fcbd23.o
  "std::__1::ctype<char>::id", referenced from:
      std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&) in hello_world-fcbd23.o
  "std::__1::locale::~locale()", referenced from:
      std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in hello_world-fcbd23.o
  "std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in hello_world-fcbd23.o
  "std::__1::ios_base::clear(unsigned int)", referenced from:
      std::__1::ios_base::setstate(unsigned int) in hello_world-fcbd23.o
  "std::terminate()", referenced from:
      ___clang_call_terminate in hello_world-fcbd23.o
  "___cxa_begin_catch", referenced from:
      ___clang_call_terminate in hello_world-fcbd23.o
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in hello_world-fcbd23.o
  "___cxa_call_unexpected", referenced from:
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >::failed() const in hello_world-fcbd23.o
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >::ostreambuf_iterator(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in hello_world-fcbd23.o
  "___cxa_end_catch", referenced from:
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in hello_world-fcbd23.o
  "___gxx_personality_v0", referenced from:
      _main in hello_world-fcbd23.o
      std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in hello_world-fcbd23.o
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in hello_world-fcbd23.o
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >::failed() const in hello_world-fcbd23.o
      std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >::ostreambuf_iterator(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in hello_world-fcbd23.o
      std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in hello_world-fcbd23.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If I run c code with gcc it works, but for some reason this c++ code isn't working. Is there a fix for this that does not require me to use Xcode?

Replies

The most likely cause of this problem is that you failed to link with the C++ standard library, but you didn’t show the command you used to compile this program so it’s hard to be sure. Regardless, check out this post for an example of how to build a trivial C++ program from the command line.

I … want to develop in the terminal rather than XCode

That’s fair enough but I still recommend that you keep Xcode to hand. It helps in situations like this, because you can create a test project from one of the built-in templates, build it, and then look at the build log to see what steps it took.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I understand that you all have very well tried everything but I have an odd suggestion. Try installing the code runner extension it worked for me.

I uninstalled X-Code and re-installed Code Runner 4, which resolved the problem... I was not using X-Code anyway...

Cheers,

Leandro Meinhardt