我尝试在osx lion上编译这个cpp代码,但是我收到了一个错误。
#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
答案 0 :(得分:89)
通常,在通过调用C前端编译C ++代码时会发生这种故障。您执行的gcc
理解并将文件编译为C ++,但不将其与C ++库链接。例如:
$ 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
(我建议),则会出现相同的行为(略有不同的消息):
$ 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
查看链接器调用以查看出现了什么问题。它会显示这个链接:
"/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++
链接行是:
"/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++
包括在内,并且是presto - 没有链接错误。
答案 1 :(得分:5)
尝试
g++ main.cpp
这样它应该可以工作,至少使用OS X
答案 2 :(得分:3)
我不熟悉OSX LION。但是,从最严格的意义上讲,所描述的错误不是由编译器引起的,而是由链接器引起的。似乎标准库没有被链接。
答案 3 :(得分:3)
使用CC命令(大写)编译C ++并链接到标准C ++库。
答案 4 :(得分:1)
从Yosemite(10.10.1)开始,我发现带有gcc
标志的-lc++
也有效:
gcc -lc++ main.cpp
答案 5 :(得分:1)
如果在OS X上使用clang,请尝试:
clang++ simple_cpp_program_file.cpp -o simple_cpp_program_file.out
答案 6 :(得分:1)
这是适用于macOs Sierra的解决方案:
OS X上有两种标准C ++库实现:libstdc ++和libc ++。它们不是二进制兼容的,libMLi3需要libstdc ++。
10.8和更早版本默认情况下选择libstdc ++,默认情况下选择10.9 libc ++。为确保与libMLi3的兼容性,我们需要手动选择libstdc ++。
为此,请将-stdlib = libstdc ++添加到链接命令。
答案 7 :(得分:0)
这个GCC是Windows(MinGW)还是Linux?在MinGW上,您需要参数-lmingw32 -enable-auto-import
。 Linux可能需要类似的东西,最有可能需要-enable-auto-import
。
答案 8 :(得分:0)
因此,错误ld:-lstdc ++找不到库是实际错误所在。
要解决此问题,请打开文件夹
打开/ Library / Developer / CommandLineTools / Packages /
运行软件包macOS_SDK_headers_for_macOS_10.14.pkg
然后gem install mini_racer起作用了!
此问题可能不仅与mini_racer有关,因为它可能会影响编译扩展的所有gem。。