共享对象的实际用途[副本]

时间:2023-02-13 10:23:11

This question already has an answer here:

这个问题已经有了答案:

What is the actual use case of shared object (.so) file ?

共享对象(.so)文件的实际用例是什么?

As per my understanding it is used to link with other higher level languages like JAVA (JNI), etc. Also it is used to create the library files for various OS flavors/processor architecture.

根据我的理解,它用于链接其他高级语言,如JAVA (JNI)等。它还用于为各种OS风格/处理器体系结构创建库文件。

How to make use of this feature ?

如何利用这个特性?

For example, if I want to create an generic executable and needs to be work in all flavors of Linux OS is the shared object file can be used ? How it is done ?

例如,如果我想创建一个通用的可执行文件,并且需要在各种Linux操作系统中工作,那么可以使用共享对象文件吗?这是怎么做到的?

2 个解决方案

#1


1  

To make use of Shared objects, First you need to create a shared library. It is also called Dynamic library.

要使用共享对象,首先需要创建一个共享库。它也被称为动态库。

The Shared libraries are used in Dynamic run time linking. That means at running time of the program these shared libraries are loaded into RAM.

共享库用于动态运行时链接。这意味着在运行时,这些共享库被加载到RAM中。

Command to create Dynamic library or Shared library-

命令创建动态库或共享库。

cc -shared -o libany_name.so file1.o file2.o file3.o

where any_name name represents you can give any name to the library. For ex- libadd.so

在any_name代表的地方,您可以为库提供任何名称。ex - libadd.so

You can execute your program with Shared library by-

你可以通过共享库来执行你的程序。

cc filename.c libany_name.so

When you create a executable file-

当你创建一个可执行文件-

cc filename.c 

or

cc filename.c libany_name.so

it will create a Dynamically Executable file. These Dynamically executable files needs some dependency files in run time. Those dependency files are automatically loaded into by Compiler.

它将创建一个动态的可执行文件。这些动态可执行文件在运行时需要一些依赖文件。这些依赖文件是由编译器自动加载的。

To avoid this go with Static library or Static Executable files. Static executable file contains all the functions itself. The size of the executable file will be more.

要避免这种情况,可以使用静态库或静态可执行文件。静态可执行文件包含所有函数本身。可执行文件的大小将会更多。

Command to create a static library-

命令创建一个静态库-

ar rcs libany_name.a file1.o file2.o file3.o

Command to create a static executable file-

命令创建一个静态可执行文件。

cc -static filename.c

#2


1  

To complement Satish's answer, ELF shared objects have generally some shared text segments (in addition of some private, unshared, segments), which are mmap(2)-ed in virtual memory using MAP_SHARED. A typical Linux system has many -hundreds- shared libraries (e.g. /usr/lib/lib*.so files), so they may share some common RAM (two different processes using the same library would typically share the RAM for its text segment) and updating them is easy (if you update some lib*.so future runs of programs linked to it will use the updated version).

为了补充Satish的回答,ELF共享对象通常具有一些共享文本段(除了一些私有的、未共享的段),它们是使用MAP_SHARED在虚拟内存中的mmap(2)-ed。典型的Linux系统有许多-数百个共享库(例如/usr/ lib*)。因此,它们可能共享一些公共RAM(使用相同库的两个不同进程通常共享其文本段的RAM),并且更新它们很容易(如果您更新某个lib*的话)。因此,未来连接到它的程序将使用更新的版本)。

Shared objects can also be dynamically loaded at runtime using dlopen(3) (then dlsym to get some symbols inside them). This is how plugins are loaded. The loading application has to define and document some conventions, e.g. requirements about symbols to be found in the plugins and how they are used.

还可以使用dlopen(3)在运行时动态加载共享对象(然后使用dlsym在其中获取一些符号)。这就是插件的加载方式。加载应用程序必须定义并记录一些约定,例如关于要在插件中找到的符号的要求以及它们是如何使用的。

A good reference on that is Drepper's paper: How to Write Shared Libraries ; see also Levine's book: Linkers and Loaders and Advanced Linux Programming.

这方面的一个很好的参考是Drepper的论文:如何编写共享库;参见Levine的书:链接器、加载器和高级Linux编程。


Your generic executable goal might be an illusion. First, there are various Linux systems running on different processors or ABI (x86 32 bits, x86-64, x32, ARM). Then, even if restricting to x86-64 Linux, various distributions have different versions of libc and of other libraries needed by your executable.

你的通用可执行目标可能是一种错觉。首先,有各种Linux系统运行在不同的处理器或ABI上(x86 32位、x86-64、x32、ARM)。然后,即使仅限于x86-64 Linux,各种发行版都有不同版本的libc和其他可执行文件所需的库。

The simplest way would be to make your program free software (so publish its source code); then it could be packaged by distribution makers. Also, it would help if you packaged your software for common distributions.

最简单的方法是使你的程序成为免费的软件(所以公布它的源代码);然后再由分发商进行包装。此外,如果您将您的软件打包成通用的发行版,将会有所帮助。

#1


1  

To make use of Shared objects, First you need to create a shared library. It is also called Dynamic library.

要使用共享对象,首先需要创建一个共享库。它也被称为动态库。

The Shared libraries are used in Dynamic run time linking. That means at running time of the program these shared libraries are loaded into RAM.

共享库用于动态运行时链接。这意味着在运行时,这些共享库被加载到RAM中。

Command to create Dynamic library or Shared library-

命令创建动态库或共享库。

cc -shared -o libany_name.so file1.o file2.o file3.o

where any_name name represents you can give any name to the library. For ex- libadd.so

在any_name代表的地方,您可以为库提供任何名称。ex - libadd.so

You can execute your program with Shared library by-

你可以通过共享库来执行你的程序。

cc filename.c libany_name.so

When you create a executable file-

当你创建一个可执行文件-

cc filename.c 

or

cc filename.c libany_name.so

it will create a Dynamically Executable file. These Dynamically executable files needs some dependency files in run time. Those dependency files are automatically loaded into by Compiler.

它将创建一个动态的可执行文件。这些动态可执行文件在运行时需要一些依赖文件。这些依赖文件是由编译器自动加载的。

To avoid this go with Static library or Static Executable files. Static executable file contains all the functions itself. The size of the executable file will be more.

要避免这种情况,可以使用静态库或静态可执行文件。静态可执行文件包含所有函数本身。可执行文件的大小将会更多。

Command to create a static library-

命令创建一个静态库-

ar rcs libany_name.a file1.o file2.o file3.o

Command to create a static executable file-

命令创建一个静态可执行文件。

cc -static filename.c

#2


1  

To complement Satish's answer, ELF shared objects have generally some shared text segments (in addition of some private, unshared, segments), which are mmap(2)-ed in virtual memory using MAP_SHARED. A typical Linux system has many -hundreds- shared libraries (e.g. /usr/lib/lib*.so files), so they may share some common RAM (two different processes using the same library would typically share the RAM for its text segment) and updating them is easy (if you update some lib*.so future runs of programs linked to it will use the updated version).

为了补充Satish的回答,ELF共享对象通常具有一些共享文本段(除了一些私有的、未共享的段),它们是使用MAP_SHARED在虚拟内存中的mmap(2)-ed。典型的Linux系统有许多-数百个共享库(例如/usr/ lib*)。因此,它们可能共享一些公共RAM(使用相同库的两个不同进程通常共享其文本段的RAM),并且更新它们很容易(如果您更新某个lib*的话)。因此,未来连接到它的程序将使用更新的版本)。

Shared objects can also be dynamically loaded at runtime using dlopen(3) (then dlsym to get some symbols inside them). This is how plugins are loaded. The loading application has to define and document some conventions, e.g. requirements about symbols to be found in the plugins and how they are used.

还可以使用dlopen(3)在运行时动态加载共享对象(然后使用dlsym在其中获取一些符号)。这就是插件的加载方式。加载应用程序必须定义并记录一些约定,例如关于要在插件中找到的符号的要求以及它们是如何使用的。

A good reference on that is Drepper's paper: How to Write Shared Libraries ; see also Levine's book: Linkers and Loaders and Advanced Linux Programming.

这方面的一个很好的参考是Drepper的论文:如何编写共享库;参见Levine的书:链接器、加载器和高级Linux编程。


Your generic executable goal might be an illusion. First, there are various Linux systems running on different processors or ABI (x86 32 bits, x86-64, x32, ARM). Then, even if restricting to x86-64 Linux, various distributions have different versions of libc and of other libraries needed by your executable.

你的通用可执行目标可能是一种错觉。首先,有各种Linux系统运行在不同的处理器或ABI上(x86 32位、x86-64、x32、ARM)。然后,即使仅限于x86-64 Linux,各种发行版都有不同版本的libc和其他可执行文件所需的库。

The simplest way would be to make your program free software (so publish its source code); then it could be packaged by distribution makers. Also, it would help if you packaged your software for common distributions.

最简单的方法是使你的程序成为免费的软件(所以公布它的源代码);然后再由分发商进行包装。此外,如果您将您的软件打包成通用的发行版,将会有所帮助。