关于静态和共享库用C链接 [英] Regarding Static and Shared Libraries Linking in C

查看:132
本文介绍了关于静态和共享库用C链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;  诠释的main()
  {
          的printf(%S,世界,你好);
          返回0;
  }  $ gcc的-o打招呼的hello.c

问:

1 - 我相信printf函数的目标文件是静态链接。这是否正确?

2 - 我应该如何配置/写code,使库文件是动态链接还是我的意思是它使用的共享库在运行时?

注:我在这个概念的初学者,所以随时纠正我只要它不有道理


解决方案

链接器采取任何发现。这通常是共享库。

在Linux系统中,你可以使用

 文件打招呼

找出来,无论是静态或动态链接。

例如。

 文件/斌/庆典


  

/斌/ bash中:ELF 64位LSB的可执行文件,X86-64,版本1(SYSV),动态链接(使用共享库),为GNU / Linux 2.6.24,BuildID [ SHA1] = 0x6dafe33f9353cbb054b1b1f7b079545992575757,剥离


,而

 文件/斌/ busybox的


  

/斌/ busybox的:ELF 64位LSB的可执行文件,X86-64,版本1(SYSV),静态的联系,为GNU / Linux 2.6.24,BuildID [SHA1] = 0xac4943b7daf7c3c204a2866ea5398f2337ff93c9,剥离


您可以强制静态链接,加入 -static 选项与gcc

 的gcc -o -static hello.c的打招呼文件打招呼


  

的/ tmp /你好:ELF 64位LSB的可执行文件,X86-64,版本1(GNU / Linux的),静态的联系,为GNU / Linux 2.6.24,BuildID [SHA1] = 0x790ec9b287fd2a276162560e5e6669ba6b73e68f,不可剥离


更新:

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

您可以在一个可执行使用动态和静态库。静态库的需要目标文件被复制到可执行文件。在另一侧,动态库(动态对象实际上)不会被复制,而是引用由所得到的二进制

更新:

有两种库,静态库(AR档案馆,请参见 男人AR

 文件/usr/lib/libnet.a/usr/lib/libnet.a:当前AR存档

和动态库(动态对象)

 文件/usr/lib/libnet.so.1.5.0/usr/lib/libnet.so.1.5.0:ELF 64位LSB的共享对象,X86-64,版本1(SYSV),动态链接,BuildID [SHA1] = 0x0c596357947e79001025b3c57be933690085dffb,剥去

您可以有两种安装在同一时间库,例如

  ls -l命令/usr/lib/libnet.*-rw-R  -  R-- 1根根207780十月2011-10-28 /usr/lib/libnet.a
-rw-R - R-- 1根根802十月28 2011 /usr/lib/libnet.la
lrwxrwxrwx 1根15根28十月2011 /usr/lib/libnet.so - > libnet.so.1.5.0
lrwxrwxrwx 1根15根28十月2011 /usr/lib/libnet.so.1 - > libnet.so.1.5.0
-rw-R - R-- 1根根92712十月2011-10-28 /usr/lib/libnet.so.1.5.0

这是 AR 存档包含一个或多个目标文件,这是由链接器,如果需要的可执行文件选中。共享对象是与子程序对象,它允许在运行时其他共享对象或可执行文件被调用。

如果你有兴趣在这个问题上,你也可以看看这个维基百科 - 库(计算)文章。

  #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?

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?

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

解决方案

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

On a Linux system, you can use

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

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

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

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

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)

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.

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

这篇关于关于静态和共享库用C链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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