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

这是我的简单测试代码,

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

并在下面编译命令

gcc -o test test.cpp

并且我在下面有一个编译错误。

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

我已经尝试了-m32以防万一,但仍然给我下面的错误。

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

gcc的版本是4.2,由XCode“命令行工具”安装。 操作系统是Mac OSX Mountain Lion。

我已经尝试过使用gcc 4.7或铿锵3.1,但同样的错误..


当然,用gcc编译cpp代码会给你错误。 你应该使用g++

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

上一篇: gcc and clang giving me an error while compiling

下一篇: Decode punched tape from image