C++ 与 glibc 和 libstdc++ 静态链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3214168/
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
Linking Statically with glibc and libstdc++
提问by themoondothshine
I'm writing a cross-platform application which is not GNU GPL compatible. The major problem I'm currently facing is that the application is linked dynamically with glibc and libstdc++, and almost every new major update to the libraries are not backwards compatible. Hence, random crashes are seen in my application.
我正在编写一个与 GNU GPL 不兼容的跨平台应用程序。我目前面临的主要问题是应用程序与 glibc 和 libstdc++ 动态链接,并且库的几乎每个新的主要更新都不向后兼容。因此,在我的应用程序中可以看到随机崩溃。
As a workaround, I distribute binaries of my application compiled on several different systems (with different C/C++ runtime versions). But I want to do without this. So my question is, keeping licensing and everything in mind, can I link against glibc and libstdc++ statically? Also, will this cause issues with rtld?
作为一种解决方法,我分发在多个不同系统(具有不同的 C/C++ 运行时版本)上编译的应用程序的二进制文件。但我想没有这个。所以我的问题是,记住许可和所有事情,我可以静态链接 glibc 和 libstdc++ 吗?另外,这会导致 rtld 出现问题吗?
采纳答案by themoondothshine
Specifying the option -static-libgcc
to the linker would cause it to link against a static version of the C library, if available on the system. Otherwise it is ignored.
指定-static-libgcc
链接器的选项将导致它链接到静态版本的 C 库(如果在系统上可用)。否则将被忽略。
回答by Vitor Py
You don't need to.
你不需要。
Copy the original libraries you linked against to a directory (../lib in this example) in your application folder.
将链接的原始库复制到应用程序文件夹中的目录(在本例中为 ../lib)。
Like:
喜欢:
my_app_install_path
my_app_install_path
- .bin
- lib
- documentation
- .bin
- 库
- 文件
Rename you app for something like app.bin. Substitute your app for a little shell script that sets the enviroment variable LD_LIBRARY_PATH to the library path (and concatenate the previous LD_LIBRARY_PATH contents, if any). Now ld should be able to find the dynamic libraries you linked against and you don't need to compile them statically to your executable.
将您的应用程序重命名为 app.bin 之类的内容。将您的应用程序替换为将环境变量 LD_LIBRARY_PATH 设置为库路径的小 shell 脚本(并连接之前的 LD_LIBRARY_PATH 内容,如果有)。现在 ld 应该能够找到您链接的动态库,并且您不需要将它们静态编译为您的可执行文件。
Remember to comply with the LGPL adding the given attribution to the libraries and pointing in the documentation where the source can be downloaded.
请记住遵守 LGPL,将给定的属性添加到库中,并在可以下载源的文档中指出。
回答by Matthew Flaschen
glibc is under the LGPL. Under section 6. of LGPL 2.1, you can distribute your program linked to the library provided you comply with one of five options. The first is to provide the source code of the library, along with the object code (source is optional, not required) of your own program, so it can be relinked with the library. You can alternatively provide a written offer of the same. Your own code does not have to be under the LGPL, and you don't have to release source.
glibc 在 LGPL 之下。在LGPL 2.1 的第 6. 节下,您可以分发链接到库的程序,前提是您符合五个选项之一。首先是提供库的源代码,以及你自己程序的目标代码(源代码是可选的,不是必需的),这样它就可以与库重新链接。您也可以提供相同的书面报价。您自己的代码不必在 LGPL 下,也不必发布源代码。
libstdc++ is under the GPL, but with a major exception. You can basically just distribute under the license of your choice without providing source for either your own code or libstdc++. The only condition is that you compile normally, without e.g. proprietary modifications or plugins to GCC.
libstdc++ 在 GPL 之下,但有一个主要的例外。您基本上可以根据您选择的许可进行分发,而无需为您自己的代码或 libstdc++ 提供源代码。唯一的条件是您可以正常编译,而无需对 GCC 进行专有修改或插件。
IANAL, and you should consider consulting one if you need real legal advice.
IANAL,如果您需要真正的法律建议,您应该考虑咨询。
回答by Zan Lynx
I must question what the heck you are doing with the poor library functions?
我必须质疑你在用糟糕的库函数做什么?
I have some cross platform software as well. It runs fine on Linux systems of all sorts. Build with the oldest version of software that you want to support. The glibc and libstdc++ libraries are really very backward compatible.
我也有一些跨平台软件。它在各种 Linux 系统上运行良好。使用您想要支持的最旧版本的软件进行构建。glibc 和 libstdc++ 库确实非常向后兼容。
I have built on CentOS 4 and run it on RHEL 6 beta. No problems. I can build on stable Debian and run it on testing.
我在 CentOS 4 上构建并在 RHEL 6 beta 上运行它。没问题。我可以在稳定的 Debian 上构建并在测试中运行它。
Now, I do sometimes have trouble with some libraries if I try to build on, say old Debian and try to run it on CentOS 5.4. That is usually due to distribution configuration choices that are different, like choosing threading or non-threading.
现在,如果我尝试在旧的 Debian 上构建并尝试在 CentOS 5.4 上运行它,我有时会遇到一些库的问题。这通常是由于不同的分发配置选择,例如选择线程或非线程。