c++ 无法在我的OSX中编译“hello world”cpp

我在我的mac上写了一个hello world程序,但是当我编译它时,编译器输出一个错误:

Undefined symbols for architecture x86_64:
“std::__1::locale::use_facet(std::__1::locale::id&) const”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::__1::ios_base::getloc() const”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::__1::basic_string, std::__1::allocator >::__init(unsigned long, char)”, referenced from:
std::__1::ostreambuf_iterator > std::__1::__pad_and_output >(std::__1::ostreambuf_iterator >, char const*, char const*, char const*, std::__1::ios_base&, char) in endian0-654ab7.o
“std::__1::basic_string, std::__1::allocator >::~basic_string()”, referenced from:
std::__1::ostreambuf_iterator > std::__1::__pad_and_output >(std::__1::ostreambuf_iterator >, char const*, char const*, char const*, std::__1::ios_base&, char) in endian0-654ab7.o
“std::__1::basic_ostream >::sentry::sentry(std::__1::basic_ostream >&)”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::__1::basic_ostream >::sentry::~sentry()”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::__1::cout”, referenced from:
_main in endian0-654ab7.o
“std::__1::ctype::id”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::__1::locale::~locale()”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::__1::ios_base::__set_badbit_and_consider_rethrow()”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::__1::ios_base::clear(unsigned int)”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“std::terminate()”, referenced from:
___clang_call_terminate in endian0-654ab7.o
“___cxa_begin_catch”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
___clang_call_terminate in endian0-654ab7.o
“___cxa_end_catch”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
“___gxx_personality_v0”, referenced from:
std::__1::basic_ostream >& std::__1::__put_character_sequence >(std::__1::basic_ostream >&, char const*, unsigned long) in endian0-654ab7.o
std::__1::ostreambuf_iterator > std::__1::__pad_and_output >(std::__1::ostreambuf_iterator >, char const*, char const*, char const*, std::__1::ios_base&, char) in endian0-654ab7.o
Dwarf Exception Unwind Info (__eh_frame) in endian0-654ab7.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是该计划:

#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    cout<< "hello world";
    return 0;
}
最佳答案
看起来您缺少(无法链接)C运行时库.虽然您没有显示用于编译和链接程序的命令行,但应该这样做:

$clang++ -o hello hello.cpp

转载注明原文:c++ 无法在我的OSX中编译“hello world”cpp - 代码日志