我尝试在 osx lion 上编译此 cpp 代码,但出现错误.
I try to compile this cpp code on osx lion but I get an error.
#include <iostream>
using namespace std;
int main (int argc, char *argv[])
{
for(int i = 0; i < 10; i++)
{
cout << "hi";
cout << endl;
}
return 0;
}
编译:
cc main.cpp
错误:
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccBdbc76.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 ccBdbc76.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 ccBdbc76.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 ccBdbc76.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccBdbc76.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccBdbc76.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
通常在通过调用 C 前端编译 C++ 代码时会发生这种故障.您执行的 gcc
将文件理解为 C++ 并将其编译为 C++,但不会将其与 C++ 库链接.示例:
Normally this sort of failure happens when compiling your C++ code by invoking the C front-end. The gcc
you execute understands and compiles the file as C++, but doesn't link it with the C++ libraries. Example:
$ gcc example.cpp
Undefined symbols for architecture x86_64:
"std::cout", referenced from:
_main in ccLTUBHJ.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 ccLTUBHJ.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 ccLTUBHJ.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 ccLTUBHJ.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccLTUBHJ.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccLTUBHJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
$ g++ example.cpp
$
如您所见,使用 g++
可以解决问题.如果您使用 clang
(我建议这样做),则会发生相同的行为(消息略有不同):
As you can see, using g++
makes the problems go away. The same behaviour (with slightly different messages) occurs if you use clang
(which I'd recommend):
$ clang example.cpp
Undefined symbols for architecture x86_64:
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in cc-IeV9O1.o
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in cc-IeV9O1.o
"std::cout", referenced from:
_main in cc-IeV9O1.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 cc-IeV9O1.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 cc-IeV9O1.o
"std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
_main in cc-IeV9O1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ clang++ example.cpp
$
正如您在 clang
错误消息中看到的那样,您可以使用 -v
来查看链接器调用以了解发生了什么问题.它会向您显示此链接行:
As you can see in the clang
error message, you could use -v
to see the linker invocation to see what's going wrong. It would show you this link line:
"/usr/bin/ld" -demangle -dynamic -arch x86_64
-macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o
/var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-hdOL8Z.o
-lSystem /Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a
或者类似的东西 - 正如你所看到的,它链接的是 C 运行时,而不是 C++,也没有 C++ 库.使用 clang++
链接行是:
Or something like it - as you can see, it's linking the C runtime, not C++, and also doesn't have the C++ libraries. Using clang++
the link line is:
"/usr/bin/ld" -demangle -dynamic -arch x86_64
-macosx_version_min 10.6.8 -o a.out -lcrt1.10.6.o
/var/folders/zl/zlZcj24WHvenScwjPFFFQE+++TI/-Tmp-/cc-wJwxjP.o
/usr/lib/libstdc++.6.dylib -lSystem
/Developer/usr/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a
如您所见,包含了 libstdc++
,并且很快 - 没有链接错误.
As you can see, libstdc++
is included, and presto - no link errors.
这篇关于编译一些简单的 C++ 代码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!