如何创建一个包含所有二进制依赖项的Linux共享库到一个文件中? [英] How to create a linux shared library including all binary dependencies into a single so file?

查看:267
本文介绍了如何创建一个包含所有二进制依赖项的Linux共享库到一个文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在linux libMySharedLibrary.so中创建一个共享库。

但是我在编译时包含了大量引用其他库的头文件。我想从这些包含的头文件中链接所有的二进制代码,这样我就可以将我的共享库分发到一个.so文件中。



我使用gcc编译。在命令行下面:

$ p $ gcc -I $ JAVA_8_HOME / include / -I $ JAVA_8_HOME / include / linux / -I./include/ - 一世。 -fPIC -o libMyLibrary.so -shared com__MyLibrary.c



头文件位于 ./ include 。我需要将生成的libMyLibrary.so与 ./include 中的头文件的二进制文件链接在一起。




解决方案


我想从这些包含的头文件中链接所有二进制代码,我的共享库放在一个.so文件中。


您无法做到您想要的正如他们的名字所暗示的那样,共享库的全部概念是 进程并加载到他们的虚拟地址动态链接器)。



您应该阅读Drepper的 如何撰写共享库 论文和程序库HowTo 。另请参阅 ld-linux(8)& elf(5)& objdump(1)& (1)& amp; ld(1)& ldd(1)。阅读调用GCC



为了简化软件的分发,您可能需要构建一些软件包(例如 .deb 一个)瞄准一些包管理器。并且您可以将您的源代码发布为免费软件(例如,在 用你自己的一个。但是你的用户仍然需要安装这些库。例如,您可以使用

  gcc -Wall -O -g -I $ JAVA_8_HOME / include / -I $ JAVA_8_HOME / include / linux / \ 
-I./include/ -I。 -fPIC -o libMyLibrary.so \
-shared com__MyLibrary.c -lother

和请检查 ldd libMyLibrary.so ,它取决于一些 libother.so。* (您的用户应该已安装使用你的 libMyLibrary.so )。



你可以编译你正在使用的所有代码(如与位置无关的代码) - 包括其他图书馆的源代码 - 进入一个单独的共享库,但不建议这样做(如果你这样做了,程序实际上可能不会在它上面的几个共享库中使用共享的低级函数)。 b
$ b

您还需要了解一个库不是一组头文件,那就是理解链接器预处理器。头文件只是描述(部分)一些C或C ++库的 API 。另请阅读Levine的 Linkers and Loaders 书。实际上,头文件声明了一些东西(类,函数,变量,类型...)并可能定义 内联函数(但不是全局函数)。您需要了解翻译单元的含义。



您可以借助 / proc / 文件系统(请参阅 proc(5)),了解某个给定进程的虚拟地址空间。例如,在终端中尝试 cat / proc / $$ / maps

书,也许是旧的 ALP (可免费下载)或更新的东西。您可以在操作系统上阅读一些教科书,例如 操作系统:三个简单的部分 (也可以免费下载)。 wiki / Source_coderel =nofollow noreferrer>源代码和构建 免费软件库(例如,从您的Linux发行版或者 github 或其他地方) 。您应该考虑使用一些构建自动化工具(例如 GNU make ninja )构建自己的库。


I'm able to create a shared library in linux libMySharedLibrary.so.

But I'm including a bunch of header files referencing other libraries when I compile. I want to link all the binary code from these included header files so I can distribute my shared library in one .so file.

I'm using gcc to compile. Below the command line:

gcc -I$JAVA_8_HOME/include/ -I$JAVA_8_HOME/include/linux/ -I./include/ -I. -fPIC -o libMyLibrary.so -shared com__MyLibrary.c

The header files are inside ./include. I need to link the generated libMyLibrary.so with the binaries of the header files inside ./include.

How?

解决方案

I want to link all the binary code from these included header files so I can distribute my shared library in one .so file.

You cannot do what you want (and that is against the whole idea of shared libraries, which, as their name imply, are shared between several processes and "loaded" into their virtual address space by the dynamic linker).

You should read Drepper's How To Write Shared Libraries paper and the Program Library HowTo. See also ld-linux(8) & elf(5) & objdump(1) & readelf(1) & ld(1) & ldd(1). Read about Invoking GCC.

To ease distribution of your software, you may want to build some package (e.g. a .deb one) targetting some package manager. And you might publish your source code as free software (e.g. on github).

You could (and perhaps should) link other lower-level shared libraries with your own one. But your user would still need to have these libraries installed. For example, you might compile with

 gcc -Wall -O -g -I$JAVA_8_HOME/include/ -I$JAVA_8_HOME/include/linux/ \
     -I./include/ -I. -fPIC -o libMyLibrary.so \
     -shared com__MyLibrary.c -lother

and check with ldd libMyLibrary.so that it depends on some libother.so.* (which your user should have installed to use your libMyLibrary.so).

You could compile all the code you are using (as position-independent code) -including the source code of "other libraries"- into a single shared library, but that is not recommended (if you did that, a program could practicaly not use some shared low-level function in several shared libraries above it).

You also need to understand that a library is not a set of header files, that is to understand the roles of the linker and of the preprocessor. Header files just describe (partly) the API of some C or C++ library. Read also Levine's Linkers and Loaders book. In practice, header files declare some stuff (classes, functions, variables, types...) and might define inline functions (but not global ones). You need to understand what translation units are.

You could, with the help of the /proc/ file system (see proc(5)), understand the virtual address space of some given process. For instance, try cat /proc/$$/maps in a terminal.

You may read some Linux programming book, perhaps the old ALP (freely downloadable) or something newer. You may read some textbook on operating systems, such as Operating Systems: Three Easy Pieces (also freely downloadable).

Look also for inspiration into the source code and build procedure of existing free software libraries (e.g. from your Linux distribution, or on github or elsewhere). You should consider using some build automation tool (e.g. GNU make, or ninja) to build your own library.

这篇关于如何创建一个包含所有二进制依赖项的Linux共享库到一个文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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