gcc加载静态库与动态库
来源:互联网 发布:腾讯邮箱企业版 域名 编辑:程序博客网 时间:2024/05/17 04:12
//hello.c#include void print_hello(){printf("HelloWorld ");}//main.c#include#include "hello.h"int main(){print_hello(); printf("%d",HELLO);return 0;}//hello.h#define HELLO 9999void print_hello();
其中 hello.c 和 main.c 在同一个目录下, hello.h在上一个目录。
先生成 xxx.o 文件
$ gcc hello.c -c ==> hello.o
$ gcc main.c -c ==> error : no hello.h
$ gcc main.c -c -I ../ ==> main.o -I 指定头文件的搜索目录( I 是大小的 i)
$ ls ==> hello.o main.o hello.c main.c
静态库的生成
$ ar cr libhello.a hello.o ==> libhello.a (注意顺序)
静态库的使用
$ gcc main.o -o main.out -lhello==> error : /usr/bin/ld: cannot find -lhello,意思是找不到库 -l 是小写的L
1.linux下默认gcc搜索是按照LD_LIBRARY_PATH来搜索的,标准库(动态库和静态库)都在 /usr/lib/ 下面
2.-lhello是一种约定,表示搜索 libhello.a 或者 libhello.so 的库,-lXXXX 即 libXXXX.a 或者 libXXXX.so
3. 可以用 -L 来指定加载库的目录
$ gcc main.o -o main.out -L . -lhello ==>出现了main.out,成功!
静态库的测试
$ rm libhello.a -f ==>程序照样能运行,说明静态库是链接进程序的!
动态库的生成
$ gcc hello.o -o libhello.so -shared ==> libhello.so
动态库的使用
$ gcc main.o -o main.out -L . -lhello ==> -L指定库所在路径,-lhello指定libhello.a 或者 libhello.so
$ ./main.out ==> error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory
动态链接库是在程序运行时候去加载的,加载的时候是从标准目录/usr/lib下去寻找的,下面是三种解决方法
1. $ cp libhello.so /usr/lib/ ==> libhello.so拷贝到/usr/lib/下去
2. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.
// export LD_LIBRARY_PATH=$LD_LIBRARY_PATH : <path of libxxxx.so>
3 . /etc/ld.so.conf 里面加一行所在库的目录,然后执行 ldconfig
/etc/ld.so.conf文件:除了标准目录(/lib和/usr/lib)之外,链接器和加载器搜索共享库时要检查的其他目录,和这个文件相关的一个命令是:ldconfig
动态库的测试
$ rm libhello.so ==>运行的时候出错,说明程序运行的时候需要库!
- gcc加载静态库与动态库
- GCC与静态库、共享库以及动态加载库
- gcc编译动态库与静态库
- 动态链接库dll的 静态加载 与 动态加载
- 动态链接库dll的 静态加载 与 动态加载
- 动态链接库dll的 静态加载 与 动态加载
- 动态链接库dll的静态加载与动态加载
- 动态链接库dll的 静态加载 与 动态加载
- gcc、动态库、静态库
- gcc生成库文件(静态库与动态库)
- GCC生成.a静态库与.so动态库
- 服务端入门《三》(gcc 链接 静态库与动态库)
- linux动态库与静态库的编译与加载
- linux动态库与静态库的编译与加载
- GCC系列: 加载静态链接库
- 静态加载与动态加载
- gcc生成静态库和动态库
- GCC生成静态库和动态库
- Java集合框架4——Map
- mysql的变量
- Ext.Window解释
- 在linux下,解决mysql长连接异常错误(SIGPIPE)
- android的NDK和java进行本地socket通信
- gcc加载静态库与动态库
- 【杂谈】PHP和J2EE比较(转)
- 通过图片url,得到Bitmap对象
- 【Effective Java】创建和销毁对象
- CentOS6.2安装maven3.2.5
- 贪心 悼念512汶川大地震遇难同胞
- 关于libcmptd.lib的问题
- 基于AWS的媒体内容服务应用架构
- Java时间处理相对时间 之 时区相关