我正在编译一个简单的"Hello world"程序hw.cpp。它看起来像这样
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
我在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
这样一直到最后两行:
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
是什么导致了这个错误?我最感兴趣的是理解为什么会发生这种情况。
是这样的
brew reinstall gcc
/usr/local/bin/g++-12 hw.cpp
我需要特别引用GNU编译器,而且我还必须事先重新安装它。