gcc and clang giving me an error while compiling

Here is my simple test code,

#include <iostream>
int main(){
    std::cout << "SAO" << std::endl; 
    return 0;
}

and compile command below

gcc -o test test.cpp

and I am having a compile error below.

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 ccMFcxIz.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 ccMFcxIz.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
  _main in ccMFcxIz.o
"std::ios_base::Init::Init()", referenced from:
  __static_initialization_and_destruction_0(int, int)in ccMFcxIz.o
"std::ios_base::Init::~Init()", referenced from:
  ___tcf_0 in ccMFcxIz.o
"std::cout", referenced from:
  _main in ccMFcxIz.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
  _main in ccMFcxIz.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 ccMFcxIz.o
"___gxx_personality_v0", referenced from:
  Dwarf Exception Unwind Info (__eh_frame) in ccMFcxIz.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I've tried putting -m32 just in case but still give me error below.

ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

Version of gcc is 4.2 which is installed by XCode "command line tool". OS is Mac OSX Mountain Lion.

I've tried with gcc 4.7 or clang 3.1 but same error..


Ofcourse, compiling cpp code with gcc will give you error. You should use g++

链接地址: http://www.djcxy.com/p/86314.html

上一篇: 谁可以给我解释一下这个?

下一篇: gcc和clang在编译时给我一个错误