简体   繁体   中英

Error on C++'s Hello World

I started learning C++ and I tried to run the classic Hello World program:

#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:

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. Use g++ instead; it knows to include the C++ run-time libraries, where those missing symbols are defined.

Thats a C compiler you need to use a C++ compiler like rob says above g++. Also if you are a beginner I would highly recommend using Microsoft visual studio express if you run a windows OS; if not then netbeans.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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