简体   繁体   English

C ++的Hello World错误

[英]Error on C++'s Hello World

I started learning C++ and I tried to run the classic Hello World program: 我开始学习C ++,并尝试运行经典的Hello World程序:

#include <iostream>

using namespace std;

int main ()
{
  cout << "Hello, World!";
  return 0;
}

but when I compile it in terminal using GCC I always get that error: 但是当我使用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

can you tell me why? 你能告诉我为什么吗?

You've compiled your program with gcc , which is a C compiler, not a C++ compiler. 您已经使用gcc编译了程序, gcc是C编译器,而不是C ++编译器。 Use g++ instead; 改用g++ ; it knows to include the C++ run-time libraries, where those missing symbols are defined. 它知道要包含C ++运行时库,其中定义了那些缺少的符号。

Thats a C compiler you need to use a C++ compiler like rob says above g++. 这是一个C编译器,你需要使用C ++编译器,比如rob在g ++之上。 Also if you are a beginner I would highly recommend using Microsoft visual studio express if you run a windows OS; 此外,如果您是初学者,如果您运行Windows操作系统,我强烈建议您使用Microsoft visual studio express; if not then netbeans. 如果不是那么netbeans。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM