简体   繁体   English

“架构 x86_64 的未定义符号”使用 g++

[英]"Undefined symbols for architechture x86_64" using g++

I am compiling a simple "Hello world" program hw.cpp.我正在编译一个简单的“Hello world”程序 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:我在 OS X 上运行。我使用以下命令编译: g++ hw.cp但是收到一条错误消息:

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.我需要专门引用 GNU 编译器,而且我还必须事先重新安装它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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