在linux平台上编译时,常会遇到目标库的疑问,有静态库也有动态库,单个理解都不太难,但是对复杂的工程而言,一旦混合到一起去,对整个工程的理解和调用,将会造成很大困扰,本文就汇总这几种常见编译结果文件的区别。

一、格式说明

linux下编译,常会遇到后缀为:.o .so .a .la .ko等格式文件,尽管linux并不以扩展名作为识别文件格式的唯一依据,但规范约定还是有的,如下:

  • .o 是目标对象文件,相当于windows中的.obj文件
  • .a 为静态库,可以是一个或多个.o合在一起,用于静态连接
  • .la 为libtool生成的共享库,其实是个配置文档。可以用$file *.la查看*.la文件,或用vi来查看。
  • .so 为共享库,类似windows平台的dll文件

补充: 还有一种扩展名为.ko 文件,不过它是Linux内核使用的动态链接文件后缀,属于模块文件,用来在Linux系统启动时加载内核模块。

二、创建实例

1、创建.o对象文件

$ gcc -c test.c

生成test.o,跳过链接对象,所以不是可执行文件。

2、创建.a静态库文件

$ ar -r libtest.a test1.o test2.o

3、创建动态库.so

$ gcc -Wall -fpic -shared test1.c test2.c -o libtest.so

上一句执行,将test1.c和test2.c编译生成动态库文件libtest.so

4、链接库文件

$ gcc -Wall -fpic -shared -Ltest test3.c -o libtest.so

编译test3.c后并与静态libtest.a链接(默认会到/usr/lib下找该文件)生成libtest.so动态库。

5、生成.la库

.la库一般通过makefile进行,当然也可以通过命令行进行,参考命令:

$libtool --mode=link gcc -o libmylib.la -rpath /usr/lib –L/usr/lib –la

libtool将会搜索libmylib.a文件,并传家libmylib.la。更多libtool帮助如下:

Tst@Tst-PC /cygdrive/d/
$ libtool --help
Usage: libtool [OPTION]... [MODE-ARG]... Provide generalized library-building support services. --config show all configuration variables
--debug enable verbose shell tracing
-n, --dry-run display commands without modifying any files
--features display basic configuration information and exit
--mode=MODE use operation mode MODE
--preserve-dup-deps don't remove duplicate dependency libraries
--quiet, --silent don't print informational messages
--no-quiet, --no-silent
print informational messages (default)
--tag=TAG use configuration variables from tag TAG
-v, --verbose print more informational messages than default
--no-verbose don't print the extra informational messages
--version print version information
-h, --help, --help-all print short, long, or detailed help message MODE must be one of the following: clean remove files from the build directory
compile compile a source file into a libtool object
execute automatically set library path, then run a program
finish complete the installation of libtool libraries
install install libraries or executables
link create a library or an executable
uninstall remove libraries from an installed directory MODE-ARGS vary depending on the MODE. When passed as first option,
`--mode=MODE' may be abbreviated as `MODE' or a unique abbreviation of that.
Try `libtool --help --mode=MODE' for a more detailed description of MODE. When reporting a bug, please describe a test case to reproduce it and
include the following information: host-triplet: i686-pc-cygwin
shell: /bin/sh
compiler: gcc
compiler flags: -g -O2 -pipe
linker: /usr/i686-pc-cygwin/bin/ld.exe (gnu? yes)
libtool: (GNU libtool) 2.4
automake: automake (GNU automake) 1.11.1
autoconf: autoconf (GNU Autoconf) 2.68 Report bugs to <bug-libtool@gnu.org>.
GNU libtool home page: <http://www.gnu.org/software/libtool/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.

参考资料:

http://wiki.ubuntu.org.cn/index.php?title=Compiling_C&variant=zh-hans

http://afreez.blog.51cto.com/59057/7338

http://blog.csdn.net/lhf_tiger/article/details/7065289

http://topic.csdn.net/u/20080124/13/c34ece2c-81a0-4818-bec4-d705aa4aecb1.html

linux下.a/.so/.la目标库区别的更多相关文章

  1. [转帖]Linux 下软链接和硬链接的区别

    Linux 下软链接和硬链接的区别 http://os.51cto.com/art/201911/605267.htm 软连接 文件是小的 只是一个链接 删除和其他处理不影响 原始文件的计数 删除源文 ...

  2. Linux下C语言编程中库的使用

    零.问题 1. 为什么要用到库? 2. 我要用一个库,但是,尼玛命令行上该怎么写呢?或者说库文件如何使用? 3. Linux的库在那些地方? 4. 什么是静态库,什么是动态库,二者有啥区别? 5. 常 ...

  3. Linux下制作和使用静态库和动态库

    概述 Linux操作系统支持的函数库分为静态库和动态库,动态库又称共享库.linux系统有几个重要的目录存放相应的函数库,如/lib /usr/lib. 静态函数库: 这类库的名字一般是libxxx. ...

  4. Linux下动态链接库和静态链接库

    第一部分:编译过程 先了解一下linux下C代码的编译过程,C代码的编译,一般分成四个阶段,包括:预编译,编译,汇编和链接,这四个阶段的分工是 预处理过程,负责头文件展开,宏替换,条件编译的选择,删除 ...

  5. linux下软链接与硬链接及其区别

    linux下创建链接命令 ln -s 软链接 这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件在另外一个位置建立一个不同的链接,这个命令最常用的参数是-s, 具体用法是:ln ...

  6. linux 下使用opengl的glut库显示和旋转BMP图片

    效果图: 这里显示的图和原图有明显的色差,目前猜测是opengl渲染时的颜色表顺序跟BMP文件里的颜色表顺序相反导致. BMP里应该是BGRBGRBRG... ,而opengl渲染时应该是按照RGBR ...

  7. linux下P2P协议(BitTorrent)-libtorrent库编译,测试

    1.libtorrent 简介,下载和编译 libtorrent简介 libtorrent是功能齐全的C ++ bittorrent的p2p协议实现,专注于效率和可伸缩性.它可以在嵌入式设备和台式机上 ...

  8. Linux下su与su -命令的区别

    在启动服务器ntpd服务时遇到一个问题 使用 su root 切换到root用户后,不可以使用service命令: 使用 su - 后,就可以使用service命令了. 原因: su命令和su -命令 ...

  9. linux下java调用.so动态库方法2: JNA

    摘自:http://blog.csdn.net/todorovchen/article/details/21319033 另请参见: http://blog.sina.com.cn/s/blog_8c ...

随机推荐

  1. PHP填坑

    这里记录下最近PHP踩过的坑,很多都是语法性错误 (1)函数结尾忘记加: 例如匿名函数 <?php $show = function($value){ echo $value."你好& ...

  2. 03Struts2基本使用流程

    Struts2基本使用流程 1.新建web工程 2.引入struts2类库 3.创建并配置Struts2的核心控制器web.xml用来拦截客户端请求并将请求转发到相应的Action类中来处理 4.创建 ...

  3. error: Bean property 'userDAO' is not writable or has an invalid setter method.

    使用Spring属性注入的方式,没有在ServiceImpl中setDao,导致程序报错 public class AddressServiceImpl implements IAddressServ ...

  4. java基础——String的常用方法

    java中String的常用方法 1.length() 字符串的长度 例:char chars[]={'a','b'.'c'}; String s=new String(chars); i nt le ...

  5. Android开发技巧一--weight属性实现视图的居中(半)显示

    面试时,一位面试官问到:“如果我想讲按钮居中显示,并且占据其父视图宽度的一半,应该怎么做到呢?”即实现这种效果: 我们使用weightSum属性和layout_weight属性实现这一要求: < ...

  6. mybatis中resultMap引发的吐血bug

    简单的讲: 问题背景:如果在写mybatis中的resultMap时,不下心将resultMapde id写成映射接口的名字,会发生什么? 结论:单元测试进度条卡住但不报错, Tomcat运行不报错, ...

  7. localStorage前端存储数据

    <!DOCTYPE html> <html> <head> <title>localStorage演示</title> <meta c ...

  8. JSP页面中的动作标识

    JSP页面中的动作标识 制作人:全心全意 包含文件标识<jsp:include> 此标识和include指令类似,用于向当前页面中包含其它的文件,且包含的文件可以是动态文件,也可以是静态文 ...

  9. python3虚拟环境应用

    python3自带虚拟环境venv,大致操作只有三步 1. 创建虚拟环境 python3 -m venv venv(名称随意) 2. 激活虚拟环境 source venv/bin/activate 3 ...

  10. Python基础(三) 数据类型

    我们首先要看的是几乎任何语言都具有的数据类型,包括字符串.整型.浮点型以及布尔类型.这些基本数据类型组成了基本控制块,从而创建的Python应用程序. 一.基本结构 1.数值: Python支持不同的 ...