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

时间:2013-08-30 01:16:25

标签: gcc xcode4 compiler-errors clang osx-mountain-lion

这是我的简单测试代码,

#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或clang 3.1但是同样的错误..

1 个答案:

答案 0 :(得分:2)

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