简体   繁体   中英

"Undefined symbols for architechture x86_64" using g++

I am compiling a simple "Hello world" program hw.cpp. It looks like this

#include <iostream>
#include <string>
using namespace std;
int main() {
  cout << "Hello world!" << endl;
  return 0;
}

I am running on OS X. I compile with the following command: g++ hw.cp But receive an error message:

ld: warning: ignoring file /usr/local/lib/libc++.so, building for macOS-x86_64 but attempting to link with file built for unknown-unsupported file format ( 0x49 0x4E 0x50 0x55 0x54 0x28 0x6C 0x69 0x62 0x63 0x2B 0x2B 0x2E 0x73 0x6F 0x2E )
Undefined symbols for architecture x86_64:
  "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 hw-a865c1.o
  "std::__1::ios_base::getloc() const", referenced from:
      std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in hw-a865c1.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 hw-a865c1.o
  "std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
      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_tr

Which goes on like that until the last two lines:

ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What could be causing this error? I am most interested in understanding why this is happening.

Here's what worked

brew reinstall gcc
/usr/local/bin/g++-12 hw.cpp

I needed to specifically reference the GNU compiler and I also had to reinstall it beforehand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM