macos GCC工具链编译简单的hello world的问题[重复]
Posted
技术标签:
【中文标题】macos GCC工具链编译简单的hello world的问题[重复]【英文标题】:Issues with macos GCC toolchain compiling simple hello world [duplicate] 【发布时间】:2020-02-08 08:29:25 【问题描述】:我在 macOS 10.14.6 上的 gcc 配置存在一些问题。
你好.cpp:
#include <iostream>
using namespace std;
int main()
cout << "Hello World.";
命令:
$ gcc -o hello hello.cpp
回复:
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 hello-1b0e0e.o
"std::__1::ios_base::getloc() const", referenced from:
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in hello-1b0e0e.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-1b0e0e.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_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in hello-1b0e0e.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-1b0e0e.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-1b0e0e.o
"std::__1::cout", referenced from:
_main in hello-1b0e0e.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-1b0e0e.o
"std::__1::locale::~locale()", referenced from:
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in hello-1b0e0e.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-1b0e0e.o
"std::__1::ios_base::clear(unsigned int)", referenced from:
std::__1::ios_base::setstate(unsigned int) in hello-1b0e0e.o
"std::terminate()", referenced from:
___clang_call_terminate in hello-1b0e0e.o
"___cxa_begin_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-1b0e0e.o
___clang_call_terminate in hello-1b0e0e.o
"___cxa_call_unexpected", referenced from:
std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >::ostreambuf_iterator(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in hello-1b0e0e.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-1b0e0e.o
"___gxx_personality_v0", 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-1b0e0e.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-1b0e0e.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-1b0e0e.o
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in hello-1b0e0e.o
Dwarf Exception Unwind Info (__eh_frame) in hello-1b0e0e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我已经阅读了这些:
Can't compile a C program on a Mac after upgrading to Catalina 10.15
GCC Missing Headers on macOS10.15 with Xcode11
Compiling with gcc in macOS Mojave
我已经安装了这个:
/Library/Developer/CommandlineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg
【问题讨论】:
你是在使用 Homebrew gcc,还是只是 Xcode 自带的 clang 别名? 【参考方案1】:变化:
$ gcc -o hello hello.cpp
到:
$ g++ -o hello hello.cpp
(否则使用 gcc,您不会自动获得所需的 C++ 库链接,并且必须明确指定它们)。
【讨论】:
好吧——那行得通。但为什么 gcc 或 clang 不起作用? 呃——谢谢你的提示!所有这些其他帖子,没有人提到它。 我刚刚添加了一个解释 - 你可以使用 gcc 但你需要使用类似gcc -lstdc++
的东西。以上是关于macos GCC工具链编译简单的hello world的问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章
linux下 gcc可以编译hello.c arm-linux-gcc 无法找到libz.so.1
C语言必须写main函数?最简单的 Hello world 你其实一点都不懂!
Ubuntu配置交叉编译环境(配置交叉编译工具链)配置arm-linux-gcc交叉编译工具链(添加交叉编译工具链(toolchain)到环境变量)