我开始学习C ++,并尝试运行经典的Hello World程序:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello, World!";
return 0;
}
但是当我使用GCC在终端中编译它时,我总是得到错误:
Undefined symbols for architecture x86_64:
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::cout", referenced from:
_main in ccpe3DPY.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccpe3DPY.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccpe3DPY.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccpe3DPY.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccpe3DPY.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
你可以告诉我为什么吗?
答案 0 :(得分:12)
您已使用gcc
编译程序,{C}编译器而不是C ++编译器。请改用g++
;它知道要包含C ++运行时库,其中定义了那些缺少的符号。
答案 1 :(得分:3)
这是一个C编译器,你需要使用像g ++上面的rob那样的C ++编译器。此外,如果您是初学者,如果您运行Windows操作系统,我强烈建议您使用Microsoft visual studio express;如果不是那么netbeans。