gcc静态库和动态库的生成以及使用
一、gcc 生成 .a 静态库和 .so 动态库
(一)编辑生成例子程序 hello.h、hello.c 和 main.c
1.打开虚拟机Ubuntu的终端
2.在合适的位置创建文件test1
mkdir test1//创建文件夹test1
cd test1//打开文件夹tes1
3.用Vim编辑器新建一个hello.c文件
vim hello.c
4.输入hello.c代码
#include <stdio.h>
void hello(const char *name)
{printf("Hello %s!\n", name); }
5.按Esc键进入命令模式,输入:wq
6.新建hello.h文件
vim hello.h
7.hello.h代码
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif //HELLO_H
按Esc后输入:wq保存退出
8.以同样的方式新建main.c文件
vim main.c
9.main.c代码
#include "hello.h"
int main() {
hello("everyone");
return 0; }
(二)将 hello.c 编译成.o 文件
1.使用工具gcc,输入命令gcc -c hello.c
2.使用命令 ls看看是否生存了 hello.o 文件
ls
3.hello.o文件存在
(三)由.o 文件创建静态库
1.输入命令创建静态库
ar -crv libmyhello.a hello.o
静态库文件名的命名规范是以 lib 为前缀,紧接着跟静态库名,扩展名为.a。
2.生成成功
3.用ls
查看静态库
(四)在程序中使用静态库
1.输入命令
gcc main.c libmyhello.a -o hello
2.运行程序
./hello
3.查看运行结果,输出hello everyone!,表示成功
(五)由.o 文件创建动态库文件
1.输入创建动态库命令
动态库文件名命名规范和静态库文件名命名规范类似,也是在动态库名增加前缀 lib,但其 文件扩展名为.so
gcc -shared -fPIC -o libmyhello.so hello.o
2.输入ls
命令查看创建结果
(六)在程序中使用动态库
1.输入命令
gcc main.c libmyhello.so -o hello
2.输入命令运行
./hello
3.原因分析
错误提示,找不到动态库文件 libmyhello.so。程序在运行时, 会在/usr/lib 和/lib 等目录中查找需要的动态库文件。若找到,则载入动态库,否则将提 示类似上述错误而终止程序运行。我们将文件 libmyhello.so 复制到目录/usr/lib 中。
4.问题解决
用管理员权限,将 libmyhello.so 复制到目录/usr/lib
输入命令sudo cp -i libmyhello.so /usr/lib
5.再次输入./hello
运行,这次成功输出
(七)静态库和动态库同名时,gcc 命令调用库文件情况
1.先删除除.c 和.h 外的所有文件,恢复成我们刚刚编辑完举例程序状态
rm -f hello hello.o /usr/lib/libmyhello.so
2.提示无法删除,原因是权限不够,用管理员权限删除
sudo rm -f hello hello.o /usr/lib/libmyhello.so
3.用ls
命令查看该文件夹下的所有文件,不存在hello、hello.o表示删除成功
3.再来创建静态库文件 libmyhello.a 和动态库文件 libmyhello.so
(1)创建静态库文件
ar -cr libmyhello.a hello.o
(2)创建动态库文件
gcc -shared -fPIC -o libmyhello.so hello.o
3.用ls
查看当前文件夹下所有文件
可以观察到存在hello.c hello.h hello.o libmyhello.a libmyhello.so main.c
4.结论
动态库和静态库同时存在时,优先使用动态库,如果直接使用命令gcc main.c libmyhello.a -o hello 的话,就是指定静态库,从程序 hello 运行的结果中很容易知道,当静态库和动态库同名时,gcc 命令将优先使用动 态库,默认去连/usr/lib 和/lib 等目录中的动态库,将文件 libmyhello.so 复制到目录/usr/lib 中即可。
二、Linux 下静态库.a 与.so 库文件的生成与使用
(一)准备工作
1.新建文件夹test2,并打开
mkdir test2
cd test2
2.新建c语言文件A1.c,使用工具Vim文本编辑器
vim A1.c
3.A1.c代码
#include <stdio.h>
void print1(int arg){
printf("A1 print arg:%d\n",arg);
}
4.以同样的方法创建c语言文件A2.c
mkdir A2.c
5.A2.c代码
#include <stdio.h>
void print2(char *arg){
printf("A2 printf arg:%s\n", arg);
}
6.创建文件A.h
vim A.h
7.A.h代码
#ifndef A_H
#define A_H
void print1(int);
void print2(char *);
#endif
8.新建c语言文件test.c
vim test.c
9.test.c代码
#include <stdlib.h>
#include "A.h"
int main(){
print1(1);
print2("test");
exit(0); }
(二)静态库.a 文件的生成与使用
1.生成目标文件
gcc -c A1.c A2.c
2.用ls
命令查看文件生成情况
3.生成静态库.a 文件
ar crv libafile.a A1.o A2.o
4.使用.a 库文件,创建可执行程序
gcc -o test test.c libafile.a
5.运行程序test输出
./test
6.结果
(三)共享库.so 文件的生成与使用
1.、生成目标文件(.o文件)
注: 此处生成.o 文件必须添加"-fpic"(小模式,代码少),否则在生成.so 文件时会出错
gcc -c -fpic A1.c A2.c
2.生成共享库.so 文件
gcc -shared *.o -o libsofile.so
3.使用.so 库文件,创建可执行程序
gcc -o test test.c libsofile.so
4.执行
./test
5.报错
./test: error while loading shared libraries: libsofile.so: cannot
open shared object file: No such file or directory
6.解决方法
用管理员权限,将libsofile.so 复制到目录/usr/lib
输入命令sudo cp -i libsofile.so /usr/lib
,根据提示输入密码
7.再执行./test
8.删除/usr/lib下的libsofile.so动态库
sudo rm -f /usr/lib/libsofile.so
三、自建函数静态库.a 与.so 库文件的生成与使用
(一)函数代码
1.入门请参考ubuntu系统下c语言helloworld入门以及编写简单程序/(一)ubuntu系统下的简单主/子程序
2.c语言文件x2x.c创建
vi x2x.c
3.x2x.c代码
#include<stdio.h>
float f1(int a,int b){
float i;
i=a+b;
printf("This is x2x.c!\n");
return i;
}
4.创建c语言文件x2y.c
vi x2y.c
4.x2y.c代码
#include<stdio.h>
float f2(int a,int b){
float i;
i=a*b;
printf("This is x2x.c!\n");
return i;
}
5.创建x.h文件
vi x.h
6.x.h代码
#ifndef x_H
#define x_H
float f1(int,int);
float f2(int,int);
#endif
~
7.创建c语言文件main.c
vi main.c
8.main.c代码
#include<stdio.h>
#include"x.h"
int main(){
int a=2,b=3;
float m,n;
m=f1(a,b);
printf("%f\n",m);
n=f2(a,b);
printf("%f\n",n);
return 0;
}
(二)静态库.a 文件的生成与使用
1.生成目标文件
gcc -c x2x.c x2y.c
2.用ls
命令查看文件生成情况,生成了x2x.o和x2y.o文件
3.生成静态库.a 文件
ar crv xy.a x2x.o x2y.o
4.使用.a 库文件,创建可执行程序
gcc -o main main.c xy.a
5.运行程序main输出
./main
(三)共享库.so 文件的生成与使用
1.、生成目标文件(.o文件)
注: 此处生成.o 文件必须添加"-fpic"(小模式,代码少),否则在生成.so 文件时会出错
gcc -c -fpic x2x.c x2y.c
2.生成共享库.so 文件
gcc -shared *.o -o xy.so
3.使用.so 库文件,创建可执行程序
gcc -o main main.c xy.so
4.执行
./main
5.报错
./main: error while loading shared libraries: xy.so: cannot open
shared object file: No such file or directory
6.解决方法
用管理员权限,将xy.so 复制到目录/usr/lib
输入命令sudo cp -i xy.so /usr/lib
,根据提示输入密码
7.再执行./main
8.删除/usr/lib下的xy.so动态库
sudo rm -f /usr/lib/xy.so
四、总结
本次实验学会如何生成静态库.a文件和动态库.os文件,函数调用静态库和动态库的方法。学会了如何生成目标文件,生成可执行的程序。当动态库和静态库重名时会优先使用动态库,这时需要把/lib下的动态库文件拷贝到urs\lib下才能正常使用