English 中文(简体)
osx上的编译错误“未定义的符号”
原标题:Compilation error "Undefined symbols" on osx
  • 时间:2011-05-29 09:52:22
  •  标签:
  • c++
  • macos
  • g++

我正在osx上尝试一个非常简单的cpp程序,只是为了熟悉osx平台,所以遇到任何错误消息我都非常惊讶。

这是代码:

#include <iostream> 
using namespace std; 
int main() { 
  cout << "Hello, world!" << endl; 
  return 0; 
}

我通过运行

gcc -Wall hello.cpp -o hello

然后我得到这个未定义的符号消息:

Undefined symbols:
  "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 ccA9oElQ.o
  "std::ios_base::Init::Init()", referenced from:
      __static_initialization_and_destruction_0(int, int)in ccA9oElQ.o
  "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 ccA9oElQ.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 ccA9oElQ.o
      std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccA9oElQ.o
      std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccA9oElQ.o
  "___gxx_personality_v0", referenced from:
      std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccA9oElQ.o
      _main in ccA9oElQ.o
      ___tcf_0 in ccA9oElQ.o
      unsigned long const& std::min<unsigned long>(unsigned long const&, unsigned long const&)in ccA9oElQ.o
      __static_initialization_and_destruction_0(int, int)in ccA9oElQ.o
      global constructors keyed to mainin ccA9oElQ.o
      CIE in ccA9oElQ.o
  "std::ios_base::Init::~Init()", referenced from:
      ___tcf_0 in ccA9oElQ.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 ccA9oElQ.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 ccA9oElQ.o
  "std::cout", referenced from:
      _main in ccA9oElQ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

我不知道它是否相关:我的MBP上安装了xcode 3和xcode 4

以下是版本信息:

$ gcc -v
Using built-in specs.
Target: i686-apple-darwin10
Configured with: /var/tmp/gcc/gcc-5666.3~6/src/configure --disable-checking --enable-werror --prefix=/usr --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin10 --program-prefix=i686-apple-darwin10- --host=x86_64-apple-darwin10 --target=i686-apple-darwin10 --with-gxx-include-dir=/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)

系统信息

$ uname -a
Darwin mbp002.local 10.7.0 Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386 i386
最佳回答

使用g++而不是gcc,因为您正在编译C++程序:

g++ -Wall hello.cpp -o hello

或者,使用clang++,因为苹果已经有一段时间没有更新GCC了,他们也不太可能更新它:

clang++ -Wall hello.cpp -o hello
问题回答

使用g运行++

g++ -Wall hello.cpp -o hello

如果使用gcc,则默认情况下不会链接C++库。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签