0. 回顾一下 gcc 选项

==============================================

-E : 仅做预处理,例如去注释,宏展开,include 展开等

-S : 将源程序编译为汇编代码 *.s

-c :  预处理,汇编,生成目标文件 *.o

-o :  预处理,汇编,链接,生成可执行 ELF 文件

==============================================

-g :  编译时加入调试信息

-Wall : 编译时显示所有告警

-std=c99 : 编译时支持 c99 语法

-I directory : (i) 加入头文件搜索目录

-lxxx : (L) 链接静态库或动态库

==============================================

1. 创建和使用静态库

- 库文件 arithmetic.c

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int div(int a, int b) { return a / b; }

- 头文件 arithmetic.h

#ifndef ARITHMETIC_H_
#define ARITHMETIC_H_ extern int add(int, int);
extern int sub(int, int);
extern int mul(int, int);
extern int div(int, int); #endif

- 主测试文件 use_arithmetic_lib.c

#include "arithmetic.h"
#include <stdio.h> int main(void)
{
int x, y;
printf("Enter two integers: ");
scanf("%d%d", &x, &y); printf("add is: %d\n", add(x, y));
printf("sub is: %d\n", sub(x, y));
printf("mul is: %d\n", mul(x, y));
printf("div is: %d\n", div(x, y)); return ;
}

生成静态库步骤:

1)编译 arithmetic.c 为目标文件,生成 arithmetic.o

$ gcc -c arithmetic.c

2)将 arithmetic.o 转换为静态库文件(注意,库文件命名规则,libxxx.a)

$ ar rcs libarithmetic.a arithmetic.o

使用静态库:

方法1:$ gcc use_arithmetic_lib.c -static ./libarithmetic.a -o use_arithmetic_lib

方法2:$ gcc use_arithmetic_lib.c -L. -larithmetic -o use_arithmetic_lib               # -L 指定静态库文件路径

正规使用静态库到方式: $ gcc use_arithmetic_lib.c  -static  -L.  -larithmetic  -o  use_arithmetic_lib

对于静态库,编译完成后,可执行文件可以脱离静态库运行。但是设想一下,如果一个静态库被调用很多次,则在内存中有这个库很多重复的加载,这是引入动态库的原因之一。以下介绍动态库。

2. 创建和使用动态库

(使用和上述静态库同样的c代码)

生成动态库步骤:

1)编译 arithmetic.c 为目标文件,生成 arithmetic.o

$ gcc -c arithmetic.c

2)将目标文件转换成动态库文件 (注意动态库文件的命名规则 libxxx.so.major.minor)

$ gcc -fPIC -shared -o libarithmetic.so.0.1 arithmetic.o

以上两步可以合在一起,

$ gcc -fPIC -shared -o libarithmetic.so.0.1 arithmetic.c

使用动态库文件:

$ gcc use_arithmetic_lib.c  -L.  -larithmetic  -o  use_arithmetic_lib

要使编译后的可执行文件能找到动态库文件,则,

1) 将动态库文件拷贝到 /usr/local/lib(建议自己生成的动态库文件放在此目录。默认动态库文件搜索路径是 /usr/lib, /lib, /usr/local/lib)

$ sudo cp libarithmetic.so.0.1 /usr/local/lib

2)创建一个软链接

$ sudo ln -sf libarithmetic.so.0.1 libarithmetic.so

3) 更新 ld 缓存

$ sudo ldconfig

使用可执行文件时,不能脱离动态库环境(例如,可执行文件不能放到其它机器上跑), run 结果如下,

=====================================================================================

有时编译完成后,执行时报错: cannot open shared object file: No such file or directory 解决方法有以下两种:

第一种: 在 .bashrc 中添加 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib  (假定动态链接文件.so 安装在 /usr/local/lib 下)

第二种: $ sudo vim /etc/ld.so.conf  添加 /usr/local/lib 然后执行 $ sudo ldconfig

=====================================================================================

动态库文件的好处是,可以动态更新库文件,不用重复编译可执行文件。如下,

更改 arithmetic.c 为 arithmetic_modify.c 为,

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int div(int a, int b) { return a / b + ; }

编译生成目标文件:

$ gcc -c arithmetic_modify.c

生成动态库文件:

$ gcc -fPIC -shared -o libarithmetic.so.0. arithmetic.o

拷贝动态库文件到 /usr/local/lib

$ sudo cp libarithmetic.so.0.2 /usr/local/lib

更新软连接

$ sudo ln -sf libarithmetic.so.0.2 libarithmetic.so

再执行一下 use_arithmetic_lib,

最后,使用命令 ldd 可以查看可执行文件的动态库依赖关系,如下,

$ ldd use_arithmetic_lib

完。

Linux 创建静态库(.a)和动态库(.so)的更多相关文章

  1. 在Linux中创建静态库.a和动态库.so

    转自:http://www.cnblogs.com/laojie4321/archive/2012/03/28/2421056.html 在Linux中创建静态库.a和动态库.so 我们通常把一些公用 ...

  2. 自己在linux上编译、链接、动态库和静态库的学习笔记

    在平常的项目中,我们都是使用公司要求的makefile.makedebug一类的文件,因此,在编译.链接.生成和链接动态库与静态库的时候,我们只是简单的使用一些已经设置的变量,只是简单的修改.添加一些 ...

  3. [转]Linux下g++编译与使用静态库(.a)和动态库(.os) (+修正与解释)

    在windows环境下,我们通常在IDE如VS的工程中开发C++项目,对于生成和使用静态库(*.lib)与动态库(*.dll)可能都已经比较熟悉,但是,在linux环境下,则是另一套模式,对应的静态库 ...

  4. Linux高级编程--02.gcc和动态库

    在Linux环境下,我们通常用gcc将C代码编译成可执行文件,如下就是一个简单的例子: 小实验:hello.c #include <stdlib.h> #include <stdio ...

  5. 《CMake实践》笔记三:构建静态库(.a) 与 动态库(.so) 及 如何使用外部共享库和头文件

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

  6. linux c编程调用系统的动态库时,要使用dlopen等函数吗?

    同问 linux c编程调用系统的动态库时,要使用dlopen等函数吗? 2012-11-27 21:55 提问者: hnwlxyzhl 我来帮他解答 满意回答 2012-12-07 09:08 li ...

  7. 静态库(.a)与动态库(.so)的简明介绍

    静态库(.a)与动态库(.so)的简明介绍 gcc有很多关于静态库,动态库的选项如-l,-L,-fPIC,-shared -Wl,-soname,看着很复杂容易混淆,其实静态库和动态库都是应需而生,只 ...

  8. linux下添加动态链接库路径、动态库加载等方法

    linux下添加动态链接库路径的方法 2017年01月20日 10:08:17 阅读数:5596   Linux共享库路径配置 Linux下找不到共享库文件的典型现象为明明已经安装某个软包(如libn ...

  9. Linux链接库一(动态库,静态库,库放在什么路径下)

    http://www.cppblog.com/wolf/articles/74928.html http://www.cppblog.com/wolf/articles/77828.html http ...

随机推荐

  1. file上传图片,base64转换、压缩图片、预览图片、将图片旋转到正确的角度

    /** * 将base64转换为文件对象 * (即用文件上传输入框上传文件得到的对象) * @param {String} base64 base64字符串 */ function convertBa ...

  2. Android的ToolBar

    ToolBar比ActionBar更加可控,自由.因此,Google 逐渐使用ToolBar来代替ActionBar. 使用ToolBar 1.要引入appCompat_v7支持 2.主题设置为NoA ...

  3. Android为TV端助力 转载自jguangyou的博客,XML基本属性大全

    android:layout_width 指定组件布局宽度 android:layout_height 指定组件布局高度 android:alpha 设置组件透明度 android:backgroun ...

  4. Screen Space Depth Varying Glow based on Heat Diffusion

    Inspired by "Interactive Depth of Field Using Simulated Diffusion on a GPU" with heat diff ...

  5. python--线程同步原语

    Threading模块是python3里面的多线程模块,模块内集成了许多的类,其中包括Thread,Condition,Event,Lock,Rlock,Semaphore,Timer等等.下面这篇文 ...

  6. C#-泛型类型(十六)

    概述 泛型类和泛型方法兼具可重用性.类型安全性和效率,这是非泛型类和非泛型方法无法实现的 泛型通常与集合以及作用于集合的方法一起使用 泛型所属命名空间:System.Collections.Gener ...

  7. JavaScript中解决计算精度丢失的问题

    在做项目之前老师就给我们封装好了一个js文件,解决计算中丢失精度的一些函数,直接引用js文件就可以使用. eg: var numA = 0.1; var numB = 0.2; alert( numA ...

  8. hivesql优化的深入解析

    转载:https://www.csdn.net/article/2015-01-13/2823530 一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map, ...

  9. jQuery设置元素的readonly和disabled属性

    jQuery的api中提供了对元素应用disabled和readonly属性的方法,如下: 1.readonly    $('input').attr("readonly",&qu ...

  10. 基于PHP的颜色生成器

    <?php  function randomColor()  {      $str = '#';      for($i = 0 ; $i < 6 ; $i++)     {      ...