编译一些简单的c++时出错代码
我尝试在 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
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;
}
To compile:
cc main.cpp
Error:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
通常这种失败发生在通过调用 C 前端编译 C++ 代码时。您执行的
gcc
会理解该文件并将其编译为 C++,但不会将其与 C++ 库链接。示例:如您所见,使用
g++
可以解决问题。如果您使用clang
(我推荐),也会发生相同的行为(消息略有不同):正如您在
clang
错误消息中看到的,您可以使用 < code>-v 查看链接器调用,看看出了什么问题。它会向您显示此链接行:或类似的内容 - 正如您所看到的,它链接的是 C 运行时,而不是 C++,并且也没有 C++ 库。使用
clang++
,链接行是:如您所见,
libstdc++
已包含在内,并且很快 - 没有链接错误。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:As you can see, using
g++
makes the problems go away. The same behaviour (with slightly different messages) occurs if you useclang
(which I'd recommend):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: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:As you can see,
libstdc++
is included, and presto - no link errors.尝试
这种方式应该可以工作,至少使用 OS X
Try
This way it should work, at least using OS X
使用CC命令(大写)编译C++并链接到标准C++库。
Use CC command (uppercase) to compile C++ and link to standard C++ library.
我不熟悉 OSX LION。然而,从最严格的意义上来说,所描述的错误不是由编译器引起的,而是由链接器引起的。似乎标准库没有被链接。
I'm not familiar with OSX LION. However, in the strictest sense, the errors described are not caused by the compiler, but by the linker. It seems as if the standard library is not being linked.
从 Yosemite (10.10.1) 开始,我发现带有
-lc++
标志的gcc
也可以工作:As of Yosemite (10.10.1), I've found that
gcc
with the-lc++
flag also works:如果在 OS X 上使用 clang,请尝试:
If using clang on OS X , try :
以下是适用于 macOS Sierra 的解决方案:
OS X 上有两种可用的标准 C++ 库实现:libstdc++ 和 libc++。它们不是二进制兼容的,libMLi3 需要 libstdc++。
在 10.8 及更早版本上,默认选择 libstdc++,在 10.9 上,默认选择 libc++。为了确保与libMLi3的兼容性,我们需要手动选择libstdc++。
为此,请将 -stdlib=libstdc++ 添加到链接命令中。
Here's the solution that works on macOs Sierra:
There are two implementations of the standard C++ library available on OS X: libstdc++ and libc++. They are not binary compatible and libMLi3 requires libstdc++.
On 10.8 and earlier libstdc++ is chosen by default, on 10.9 libc++ is chosen by default. To ensure compatibility with libMLi3, we need to choose libstdc++ manually.
To do this, add -stdlib=libstdc++ to the linking command.
这是 Windows (MinGW) 还是 Linux 上的 GCC?在 MinGW 上,您需要参数
-lmingw32 -enable-auto-import
。 Linux 可能需要类似的东西,很可能需要-enable-auto-import
。Is this GCC on Windows (MinGW) or Linux? On MinGW you need the parameters
-lmingw32 -enable-auto-import
. Linux might needs something similar,-enable-auto-import
is most likely needed.因此,错误 ld: library not found for -lstdc++ 是实际错误所在。
要解决此问题,请打开文件夹
open /Library/Developer/CommandLineTools/Packages/
运行包 macOS_SDK_headers_for_macOS_10.14.pkg
然后 gem install mini_racer 就可以了!
这个问题可能不仅仅与 mini_racer 有关,它可能会影响任何编译扩展的 gem。
So the error ld: library not found for -lstdc++ is where the actual error lies.
To fix this, open the folder
open /Library/Developer/CommandLineTools/Packages/
Run the package macOS_SDK_headers_for_macOS_10.14.pkg
Then gem install mini_racer works!
This issue may not be only related to mini_racer as it could affect any gem that compiles an extension.。