linux下生成.so文件和.a文件
2016-07-18 14:59
573 查看
Linux库文件.a .so
1..o 就相当于windows里的obj文件 ,一个.c或.cpp文件对应一个.o文件
.a 是好多个.o合在一起,用于静态连接 ,即STATIC mode,多个.a可以链接生成一个exe的可执行文件
.so 是shared object,用于动态连接的,和windows的dll差不多,使用时才载入。
得到了ts:error while loading shared libraries: libs.so: cannot open shared object file: No such file or directory 系统不能找到我们自己定义的libs.so,那么告诉他,修改变量LD_LIBRARY_PATH。
2. 怎么生成so动态库文件?
编译:得到输出文件libs.o
gcc -fPIC -g -c s.c -o libs.o
链接:得到输出文件libs.so
gcc -g -shared -Wl,-soname,libs.so -o libs.so libs.o -lc
得到了ts:error while loading shared libraries: libs.so: cannot open shared object file: No such file or directory 系统不能找到我们自己定义的libs.so,那么告诉他,修改变量LD_LIBRARY_PATH。
3. 怎么生成a静态库文件?
编译:得到输出文件libs.o
gcc -fPIC -g -c s.c -o libs.o
ar r .a .o
4.
看.a结构,找其中的原文件,用ar -t YourFile.a
看动态库用 nm -d lib*.so
linux下生成.so文件和.a文件
test.h1 #ifndef _TEST_H_ 2 #define _TEST_H_ 3 4 void TestA(); 5 void TestB(); 6 7 #endif
test_a.cpp
1 #include <stdio.h> 2 #include "test.h" 3 4 void TestA() 5 { 6 printf("TestA func\n"); 7 }
test_b.cpp
1 #include <stdio.h> 2 #include "test.h" 3 4 void TestB() 5 { 6 printf("TestB func\n"); 7 }
生成so文件的命令
g++ test_a.cpp test_b.cpp -fPIC -shared -o libtest.so
生成.a文件的命令
1 gcc -c test_a.cpp 2 gcc -c test_b.cpp 3 ar -r libtest.a test_a.o test_b.o
test.cpp
1 #include "test.h" 2 3 int main() 4 { 5 TestA(); 6 TestB(); 7 8 return 0; 9 }
采用动态库编译命令
g++ test.cpp -o test -L. -ltest
执行
export LD_LIBRARY_PATH=./ ./test
执行结果如下。
采用静态库编译命令
g++ -static -o test -L. -ltest test.cpp
执行效果
静态库的嵌套调用,有时候我想做一个自己的静态库,它里面要调用其他静态库里面的函数,经过试验
这个好像用ar -r不行,所以就在链接的时候需要两个库文件都包含,同时要有这一个头文件才行。。。
相关文章推荐
- Linux基础(15)文件访问控制列表
- Linux环境下静态库的生成和使用 (.a文件)
- Linux下的输出重定向
- linux系统中怎么查看cpu信息?
- linux面试点
- 【C语言】 Linux下编译提示pow未定义引用
- Linux C 收藏
- linux Centos7安装mysql
- C++相对路径转换为绝对路径和linux、window移植api
- Linux下如何进入MySQL(针对自己的公司)
- 让linux中的程序崩溃时生成core文件
- CentOS 6.5 服务开机自启动
- 《linux学习》之samba服务器
- Linux C 静态库(.a) 与 动态库(.so) 的详解
- Linux系统查看硬件信息命令总结
- linux 学习
- Centos7 关闭防火墙(转)
- Linux查看文件夹大小命令
- phpStudy for Linux (lnmp+lamp一键安装包)
- liunx 系统注意危险的十大命令---谨慎使用