C++ 与旧版本的 libstdc++ 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2085427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Link with an older version of libstdc++
提问by Pieter
After installing a new build machine, I found out it came with 6.0.10 of the standard C++ library
安装新的构建机器后,我发现它带有标准 C++ 库的 6.0.10
-rw-r--r-- 1 root root 1019216 2009-01-02 12:15 libstdc++.so.6.0.10
Many of our target machines, however, still use an older version of libstdc++, for example:
然而,我们的许多目标机器仍然使用旧版本的 libstdc++,例如:
-rwxr-xr-x 1 root root 985888 Aug 19 21:14 libstdc++.so.6.0.8
Apparently the ABI changed in those last two 0.0.1's, as trying to run a program results in
显然,最后两个 0.0.1 中的 ABI 发生了变化,因为尝试运行程序会导致
/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.9' not found
I tried explicitly installing an older version of gcc but that didn't help. Upgrading the target machines is out of my control, so not an option. What's the best way to get my builds to work on machines with an older libstdc++?
我尝试明确安装旧版本的 gcc 但这没有帮助。升级目标机器不在我的控制范围内,所以不是一个选项。让我的构建在具有较旧 libstdc++ 的机器上工作的最佳方法是什么?
I searched in apt-cache for older libstdc++ versions to install, but apparently no older versions of 6 are available?
我在 apt-cache 中搜索了要安装的旧 libstdc++ 版本,但显然没有旧版本的 6 可用?
采纳答案by Jan
You don't need to link to a different library, you need to use an older version of the compiler.
您不需要链接到不同的库,您需要使用旧版本的编译器。
Have a look at the GNU ABI policy. The libstdc++ shared library is designed to be forward compatible. I.e. version 6.0.10 can be used if you need 6.0.8. In the policy you can read that from gcc-4.2.0 on, 6.0.9 is needed, so you need a gcc-4.1.x.
查看GNU ABI 政策。libstdc++ 共享库旨在向前兼容。如果您需要 6.0.8,可以使用 6.0.10 版本。在政策中,您可以从 gcc-4.2.0 开始阅读,需要 6.0.9,因此您需要 gcc-4.1.x。
In short, that's why there's only one libstdc++.so.6.0.x on your system, you only need the latest.
简而言之,这就是为什么您的系统上只有一个 libstdc++.so.6.0.x 的原因,您只需要最新的。
As for setting up your build system to use only a specific version of the compiler: make sure the standard g++ can't be used (rename the link, remove the package providing it, take it out of PATH), and start digging. Worked for me.
至于将构建系统设置为仅使用特定版本的编译器:确保无法使用标准 g++(重命名链接,删除提供它的包,将其从 PATH 中取出),然后开始挖掘。为我工作。
回答by paxdiablo
You can ship the required shared libraries with your own code (in a subdirectory for instance) and set LD_LIBRARY_PATH, as a precursor to running your application, to first search in that directory.
您可以使用您自己的代码(例如在子目录中)提供所需的共享库,并设置 LD_LIBRARY_PATH 作为运行应用程序的先导,首先在该目录中搜索。
Shipping the specific version that you need will mean that it won't matter which version the user has installed. You just have to make sure you ship all dependencies as well.
提供您需要的特定版本意味着用户安装的版本无关紧要。你只需要确保你也发布了所有依赖项。
回答by Pieter
Al alternative, which didn't work for me, but maybe someone else will find it useful, is to statically link libgcc and libstdc++.
另一种对我不起作用但也许其他人会发现它有用的替代方法是静态链接 libgcc 和 libstdc++。
gcc has an option -static-libgcc
, but simply using this option achieves nothing, as libstdc++ is still dynamically linked. But by making sure gcc can only find the static version of libstdc++, static linking can be achieved.
gcc 有一个 option -static-libgcc
,但是仅仅使用这个选项没有任何效果,因为 libstdc++ 仍然是动态链接的。但是通过确保 gcc 只能找到 libstdc++ 的静态版本,就可以实现静态链接。
ln -s `g++ -print-file-name=libstdc++.a`
g++ -static-libgcc -L. source.cpp
Problem is, the boost libraries were built against the newer libstdc++, so while the program compiles correctly, it generates runtime errors...
问题是,boost 库是针对较新的 libstdc++ 构建的,因此当程序正确编译时,它会生成运行时错误......
Maybe this is solvable if I rebuild boost too, haven't tried that.
如果我也重建 boost,也许这是可以解决的,还没有尝试过。
(for the record, if you use any code which dynamically loads libraries, via e.g. dlopen
, statically linking is all out of the question)
(作为记录,如果您使用任何动态加载库的代码,例如dlopen
,静态链接都是不可能的)
回答by Didier Trosset
Already encountered this. I didn't thought of any better than installing a system (virtualized?) with the same configuration as the target machines to build the distributable binaries.
已经遇到这个了。我没有想到比安装与目标机器具有相同配置的系统(虚拟化?)更好的方法来构建可分发的二进制文件。
回答by just somebody
You can either "smuggle" the newer libstdc++ into client systems (into a private area) and link the programs with appropriate -rpath
, or you can get an older version of libstdc++ onto your computer. It doesn't look like you needthe update, and it may be out of question for other reasons anyway.
您可以将较新的 libstdc++“走私”到客户端系统(进入私有区域)并将程序与适当的链接-rpath
,或者您可以将旧版本的 libstdc++ 安装到您的计算机上。看起来您不需要更新,并且无论如何可能由于其他原因而无济于事。
Note: on FreeBSD, libstdc++ is coupled with the compiler (I have gcc4.2, 4.4 and 4.5 installed, each with it's own libstc++). Try installing an older (matching the client systems) version of gcc, it might carry the older libstdc++ you're looking for.
注意:在 FreeBSD 上,libstdc++ 与编译器耦合(我安装了 gcc4.2、4.4 和 4.5,每个都有自己的 libstc++)。尝试安装较旧的(匹配客户端系统)版本的 gcc,它可能带有您正在寻找的较旧的 libstdc++。
回答by Tom
Have you tried just putting it in with the list of sources? This assumes you actually have the library installed!
您是否尝试将其与来源列表一起放入?这假设您确实安装了库!
g++ /usr/lib/libstdc++.so.6.0.8 source1.cpp source2.cpp
回答by Rodrigo
Work for me: set this flags:
为我工作:设置这个标志:
-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic -Wl,-Bstatic -lgcc -Wl,-Bdynamic -static
-Wl,-Bstatic -lstdc++ -Wl,-Bdynamic -Wl,-Bstatic -lgcc -Wl,-Bdynamic -static
If put only -static-libstdc++ -static not work to me.
如果只放 -static-libstdc++ -static 对我不起作用。