如何使用静态链接到其中的所有库创建 .so 文件? [英] How do I create .so files with all libraries statically linked into it?

查看:60
本文介绍了如何使用静态链接到其中的所有库创建 .so 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 .so 文件,其中所有库(GMP 和 PROTOBUF)都静态链接到其中,以便它可以在其他系统中使用而无需安装这些库.

I want to create a .so file with all libraries (GMP and PROTOBUF) statically linked into it so that it can be used in other systems without installing those libraries.

这就是我为 MAC 所做的:

This is what I did for MAC:

gcc -I /usr/local/include -shared -o xyz.so -g xyz.c /usr/local/lib/libprotobuf-c.a /usr/local/lib/libgmp.a

但它不适用于 Linux.如何使其适用于 Linux?

But it doesn't work for Linux. How can I make it work for Linux?

推荐答案

A shared 应该(在 Linux 上)实际上包含 位置无关代码(图片).因此,从单个源文件xyz.c构建共享库libxyz.so的正确方法是

A shared library should (on Linux) practically contain position-independent code (PIC). So the correct way to build a shared library libxyz.so out of a single source file xyz.c is

gcc -Wall -g -shared -I/usr/local/include -o libxyz.so -fPIC xyz.c

(你可以 - 并且可能想要 - 添加 rpath 相关选项)

(and you could -and probably want to- add rpath related options)

静态库不包含 PIC(除非它是专门这样构建的,这在 Linux 上非常罕见;在 Debian 上,我知道的唯一示例是特殊的 libc6-pic 包提供以促进定制和扩展 libc*so 的构建.

A static library don't contain PIC (unless it was specially built as such, which is very uncommon on Linux ; on Debian the only example I know is the special libc6-pic package provided to facilitate build of customized and extended libc*so).

您需要链接这些库的 PIC 变体;所以你应该从他们的源代码中构建它们.

You need to link PIC variants of these libraries; so you should build them from their source code.

阅读 Drepper 的 如何编写共享库更多,还有Program Library HowTo.

Read Drepper's How To Write Shared Libraries for more, and also Program Library HowTo.

(在一些理论上的奇怪情况下,您可以链接一个不包含 PIC 的共享库,但这会使它们的 动态链接 非常慢,因为很多重定位 将被处理,并且它们的代码段不会在多个进程之间通过适当调用mmap(2)完成ld-linux(8);这是不推荐,非常脆弱,而且通常不能很好地工作;我从来没有这样做过)

(In some few theoretical weird cases, you could link a shared library which don't contain PIC, but that will make their dynamic linking extremely slow, since a lot of relocations would be processed and their code segment won't be shared -between several processes- by appropriate calls to mmap(2) done by ld-linux(8); this is not recommended at all, is very brittle, and often won't work well; I never did that)

我想创建一个 .so 文件,其中所有库(GMP 和 PROTOBUF)都静态链接到其中

I want to create a .so file with all libraries (GMP and PROTOBUF) statically linked into it

不要那样做.我解释了原因.这样做违反了 Linux 上的(社交)规则(因为您的用户不希望拥有无数的 libgmp 变体等......).

Don't do that. I explained why. And doing that is against the (social) rules on Linux (because your users don't want to have zillions of variants of libgmp, etc...).

但是,您可以将共享库与之前的其他一些(更基本的)共享库链接起来.在你的情况下,我建议这样做.所以要求你的用户已经安装了 GMP 和 PROTOBUF 包.

You can however link a shared library with some previous other (more basic) shared libraries. In your case, I recommend doing that. So require your user to have installed GMP and PROTOBUF packages.

考虑还为您的(或常见的)Linux 发行版(例如.deb Ubuntu 或 Debian 文件)

Consider also providing packages for your (or common) Linux distributions (e.g. a .deb file for Ubuntu or Debian)

或者,将您的库代码发布为免费软件;然后,您可能希望有几个用户下载它的源代码,也许(最终)某个发行版会打包它.你可以等待数年才能实现;或者至少你得到了打包的帮助.

Alternatively, publish your library code as free software; you might then hope that several users would download its source, and perhaps (eventually) some distribution would package it. You could wait years for that to happen; or at least you get help to package it.

这篇关于如何使用静态链接到其中的所有库创建 .so 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆