我正在尝试使用c ++。我在安装了Mavericks OS的Mac上。
我正在尝试编译的程序是:
#include <iostream>
int main()
{
std::cout << "Enter two numbers:" << std::endl;
return 0;
}
这是cc的版本:
Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin13.0.0
Thread model: posix
然而,当我尝试编译时:
cc test.cc
我收到以下错误:
...
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test-AeQQK5.o
___clang_call_terminate in test-AeQQK5.o
"___cxa_end_catch", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test-AeQQK5.o
"___gxx_personality_v0", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::operator<<<std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*) in test-AeQQK5.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 test-AeQQK5.o
Dwarf Exception Unwind Info (__eh_frame) in test-AeQQK5.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我做错了什么?
答案 0 :(得分:2)
您正在将其编译为C程序。将其编译为C ++程序:
c ++ test.cc
最好抛出几个警告标志:
c ++ -Wall -Wextra -pedantic-errors test.cc
答案 1 :(得分:1)
您需要运行clang++
,而不仅仅是clang
。
答案 2 :(得分:1)
虽然使用gcc编译C ++源代码* .cc(它将该扩展名作为C ++源代码处理)是完全正常的,但链接器也需要显式信息来链接正确的c ++库。
(注意:我认为clang和gcc在此问题上相似)