关于C中链接的静态和共享库

时间:2022-09-18 11:43:07
  #include <stdio.h>

  int main()
  {
          printf("%s","Hello world");
          return 0;      
  }

  $gcc -o hello hello.c

Question:

问题:

1 - I believe the printf function's object file is statically linked. Is that correct?

1 -我认为printf函数的对象文件是静态链接的。那是正确的吗?

2 - How should I configure/write this code so that the library files are dynamically linked or I mean it uses shared libraries at runtime?

2 -如何配置/编写此代码,以便库文件被动态链接,或者我的意思是它在运行时使用共享库?

Note: I am beginner in this concept, so feel free to correct me wherever it doesn't makes sense

注意:我是这个概念的初学者,所以请随时纠正我的错误

1 个解决方案

#1


3  

The linker takes whatever it finds. This is usually the shared library.

链接器接受它找到的任何东西。这通常是共享库。

On a Linux system, you can use

在Linux系统上,您可以使用

file hello

to find out, whether it is linked statically or dynamically.

要找出,它是静态的还是动态的。

E.g.

如。

file /bin/bash

gives

给了

/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x6dafe33f9353cbb054b1b1f7b079545992575757, stripped

/bin/bash: ELF 64位LSB可执行文件,x86-64,版本1 (SYSV),动态链接(使用共享的libs),对于GNU/Linux 2.6.24, BuildID[sha1]=

whereas

file /bin/busybox

gives

给了

/bin/busybox: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0xac4943b7daf7c3c204a2866ea5398f2337ff93c9, stripped

/bin/busybox: ELF 64位LSB可执行文件,x86-64,版本1 (SYSV)静态链接,对于GNU/Linux 2.6.24, BuildID[sha1]=0xac4943b7daf7c3c204a2866ea5398f2337ff93c9,删除

You can force a static link, by adding the -static option to gcc

您可以通过向gcc添加-static选项来强制一个静态链接

gcc -static -o hello hello.c

file hello

/tmp/hello: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0x790ec9b287fd2a276162560e5e6669ba6b73e68f, not stripped

/tmp/hello: ELF 64位LSB可执行文件,x86-64,版本1 (GNU/Linux),静态链接,对于GNU/Linux 2.6.24, BuildID[sha1]=0x790ec9b287fd2a276162560e5e6669ba6b73e68f,未被删除。

Update:

更新:

Linking is the process of putting object files, dynamic and static libraries and some boilerplate objects together, to form a binary executable file.

链接是将对象文件、动态和静态库和一些样板对象放在一起,形成二进制可执行文件的过程。

You can use both dynamic and static libraries in one executable. The needed object files of a static library are copied into the executable. On the other side, dynamic libraries (dynamic objects actually) are not copied, but rather referenced by the resulting binary.

您可以在一个可执行文件中同时使用动态库和静态库。静态库所需的对象文件被复制到可执行文件中。另一方面,动态库(实际上是动态对象)不会被复制,而是被生成的二进制文件引用。

Update:

更新:

There are two kinds of libraries, static libraries (ar archives, see man ar)

有两种图书馆,静态图书馆(ar archive, see man ar)

file /usr/lib/libnet.a

/usr/lib/libnet.a: current ar archive

and dynamic libraries (dynamic objects)

和动态库(动态对象)

file /usr/lib/libnet.so.1.5.0 

/usr/lib/libnet.so.1.5.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0x0c596357947e79001025b3c57be933690085dffb, stripped

You can have both types of library installed at the same time, e.g.

你可以同时安装这两种类型的库。

ls -l /usr/lib/libnet.*

-rw-r--r-- 1 root root 207780 Okt 28  2011 /usr/lib/libnet.a
-rw-r--r-- 1 root root    802 Okt 28  2011 /usr/lib/libnet.la
lrwxrwxrwx 1 root root     15 Okt 28  2011 /usr/lib/libnet.so -> libnet.so.1.5.0
lrwxrwxrwx 1 root root     15 Okt 28  2011 /usr/lib/libnet.so.1 -> libnet.so.1.5.0
-rw-r--r-- 1 root root  92712 Okt 28  2011 /usr/lib/libnet.so.1.5.0

An ar archive contains one or more object files, which are selected by the linker if needed by the executable file. A shared object is an object with subroutines, which allows to be called by other shared objects or executables at runtime.

ar存档包含一个或多个对象文件,如果可执行文件需要,链接器将选择这些文件。共享对象是具有子例程的对象,它允许在运行时被其他共享对象或可执行程序调用。

If you're interested in this subject, you can also look at this Wikipedia - Library (computing) article.

如果你对这个主题感兴趣,你也可以看看*上的“图书馆(计算)”文章。

#1


3  

The linker takes whatever it finds. This is usually the shared library.

链接器接受它找到的任何东西。这通常是共享库。

On a Linux system, you can use

在Linux系统上,您可以使用

file hello

to find out, whether it is linked statically or dynamically.

要找出,它是静态的还是动态的。

E.g.

如。

file /bin/bash

gives

给了

/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=0x6dafe33f9353cbb054b1b1f7b079545992575757, stripped

/bin/bash: ELF 64位LSB可执行文件,x86-64,版本1 (SYSV),动态链接(使用共享的libs),对于GNU/Linux 2.6.24, BuildID[sha1]=

whereas

file /bin/busybox

gives

给了

/bin/busybox: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0xac4943b7daf7c3c204a2866ea5398f2337ff93c9, stripped

/bin/busybox: ELF 64位LSB可执行文件,x86-64,版本1 (SYSV)静态链接,对于GNU/Linux 2.6.24, BuildID[sha1]=0xac4943b7daf7c3c204a2866ea5398f2337ff93c9,删除

You can force a static link, by adding the -static option to gcc

您可以通过向gcc添加-static选项来强制一个静态链接

gcc -static -o hello hello.c

file hello

/tmp/hello: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.24, BuildID[sha1]=0x790ec9b287fd2a276162560e5e6669ba6b73e68f, not stripped

/tmp/hello: ELF 64位LSB可执行文件,x86-64,版本1 (GNU/Linux),静态链接,对于GNU/Linux 2.6.24, BuildID[sha1]=0x790ec9b287fd2a276162560e5e6669ba6b73e68f,未被删除。

Update:

更新:

Linking is the process of putting object files, dynamic and static libraries and some boilerplate objects together, to form a binary executable file.

链接是将对象文件、动态和静态库和一些样板对象放在一起,形成二进制可执行文件的过程。

You can use both dynamic and static libraries in one executable. The needed object files of a static library are copied into the executable. On the other side, dynamic libraries (dynamic objects actually) are not copied, but rather referenced by the resulting binary.

您可以在一个可执行文件中同时使用动态库和静态库。静态库所需的对象文件被复制到可执行文件中。另一方面,动态库(实际上是动态对象)不会被复制,而是被生成的二进制文件引用。

Update:

更新:

There are two kinds of libraries, static libraries (ar archives, see man ar)

有两种图书馆,静态图书馆(ar archive, see man ar)

file /usr/lib/libnet.a

/usr/lib/libnet.a: current ar archive

and dynamic libraries (dynamic objects)

和动态库(动态对象)

file /usr/lib/libnet.so.1.5.0 

/usr/lib/libnet.so.1.5.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=0x0c596357947e79001025b3c57be933690085dffb, stripped

You can have both types of library installed at the same time, e.g.

你可以同时安装这两种类型的库。

ls -l /usr/lib/libnet.*

-rw-r--r-- 1 root root 207780 Okt 28  2011 /usr/lib/libnet.a
-rw-r--r-- 1 root root    802 Okt 28  2011 /usr/lib/libnet.la
lrwxrwxrwx 1 root root     15 Okt 28  2011 /usr/lib/libnet.so -> libnet.so.1.5.0
lrwxrwxrwx 1 root root     15 Okt 28  2011 /usr/lib/libnet.so.1 -> libnet.so.1.5.0
-rw-r--r-- 1 root root  92712 Okt 28  2011 /usr/lib/libnet.so.1.5.0

An ar archive contains one or more object files, which are selected by the linker if needed by the executable file. A shared object is an object with subroutines, which allows to be called by other shared objects or executables at runtime.

ar存档包含一个或多个对象文件,如果可执行文件需要,链接器将选择这些文件。共享对象是具有子例程的对象,它允许在运行时被其他共享对象或可执行程序调用。

If you're interested in this subject, you can also look at this Wikipedia - Library (computing) article.

如果你对这个主题感兴趣,你也可以看看*上的“图书馆(计算)”文章。