动态库、静态库初试
2016-10-09 11:44
134 查看
//main.c 1 #include 2 #include 3 void hello(char *str) 4 { 5 printf("hello world!%s\n",str); 6 } //hello.h 3 void hello(char *str); //hello.c 1 #include 2 #include 3 void hello(char *str) 4 { 5 printf("hello world!%s\n",str); 6 }
静态库实在程序编译阶段已经连接到目标代码中,在实际程序运行的时候不需要再次使用该静态库;
动态库(共享库):编译阶段不需要连接,只要包含头文件包含所需要的函数、变量声明即可,在程序运行阶段载入动态库;
举例;三个文件---main.c;hello.c;hello.h,
//main.c 1 #include<stdio.h> 2 #include<string.h> 3 void hello(char *str) 4 { 5 printf("hello world!%s\n",str); 6 } //hello.h 3 void hello(char *str); //hello.c 1 #include<stdio.h> 2 #include<string.h> 3 void hello(char *str) 4 { 5 printf("hello world!%s\n",str); 6 }1.1——生成.o文件
gcc -c hello.c 静态库和动态库都需要通过此文件生成。
1.2——生成静态库,前缀为lib,后缀.a
ar cr libhello.a hello.o ls一下可以看到生成了libhello.a文件
1.3——使用静态库
gcc -o main main.c -L. -lhello 生成可执行文件main ,删除静态库,运行可执行文件运行正常
2.1——系统不同,需要重新生成.o文件
gcc -c -fpic hello.c
2.2——生成动态库
gcc -shared -fpic -o libhello.so heiio.o 生成动态库文件
2.3——使用动态库
gcc -o main man,c -L. -lhello 可以生成可执行文件,运行会出现错误:
./main: error while loading shared libraries: libhello.so: cannot open shared object file: No such file or directory
cp libhello.so /lib,运行./main 结果正常
相关文章推荐
- 利用gcc生成静态库和动态库
- 静态库,动态库的调用
- 动态库与静态库的区别
- 动态库与静态库【linux】
- 【C/C++学院】0801-重定向以及文件扫描/二进制加密解密/简单加密/按照密码加密/动态库与静态库
- C++静态库与动态库
- 静态库和动态库
- 技巧:Linux 动态库与静态库制作及使用详解
- Linux静态库和动态库学习总结
- Linux下用gcc生成静态库和动态库
- C++静态库与动态库
- 静态库和动态库的生成实例
- IOS-24-iOS动态库与静态库的区别及原理
- Linux系统中“动态库”和“静态库”那点事儿
- gcc生成静态库和动态库
- linux 文本命令和eclipse下开发静态库和动态库
- linux 的生成动态库,静态库
- android动态库和静态库的编译
- 在Linux下如何使用GCC编译程序、简单生成静态库及动态库。
- iOS 静态库,动态库与 Framework