目录
一、生成静态库和动态库
(1)第一步:编译生成例子程序hello.h、hello.c、main.c
创建一个新目录
代码如下:
hello.h
#ifndef HELLO_H
#define HELLO_H
void hello(const char *name);
#endif//HELLO_H
hello.c
#include<stdio.h>
void hello(const char *name)
{
printf("Hello %s\n",name);
}
main.c
#include"hello.h"
int main()
{
hello("everyone");
return 0;
}
(2)第二步,将hello.c编译成hello.o文件
gcc -c hello.c
(3)第三步,由.o文件创建静态库
ar -crv libmyhello.a hello.o
使用 ls 命令查看是否生成libmyhello.a 文件
ls
(4)第四步,在程序中使用静态库
方法一:
gcc -o hello main.c -L. -lmyhello
方法二:
gcc main.c libmyhello.a -o hello
方法三:
先生成main.o
gcc -c main.c
在生成可执行文件
gcc -o hello main.o libmyhello.a
运行文件:
./hello
hello everyone!
(5)第五步,由.o文件创建动态库文件
gcc -shared -fPIC -o libmyhello.so hello.o
使用 ls 命令查看是否生成libmyhello.so文件
(6)第六步,在程序中使用动态库
gcc -o hello main.c -L. -lmyhello
运行文件:
./hello
./hello: error while loading shared libraries:libmyhello.so:cannot
open shar ed obiect file: No such file or directory
报错了。看看错误提示,找不到动态库文件libmyhello.so 。程序在运行时,会在/usr/lib 和/lib 等目录中寻找需要的动态库文件。若找到,则载入动态库,否则将提示类似上述错误而终止程序运行。将文件libmyhello.so复制到目录/usr/lib 中,在试试。
mv libmyhello.so /usr/lib
再运行:
./hello
hello everyone!
成功了!进一步说明了动态库再程序运行时是需要的。
实机演示如下图:
二、linux下静态库.a与.so文件的生成与使用
创建新目录,并再创建的新目录中生成四个文件A1.c 、A2.c、 A.h、test.c
A1.c
#include<stdio.h>
void print1(int arg)
{
printf("A1 print arg:%d\n",arg);
}
A2.c
#include<stdio.h>
void print2(char *arg)
{
printf("A2 printf arg:%s\n",arg);
}
A.h
#ifndef A_H
#define A_H
void print1(int);
void print2(char *);
#endif
test.c
#include<stdio.h>
#include"A.h"
int main()
{
print1(1);
print2("test");
exit(0);
}
1、静态库.a文件的生成
生成目标文件(xxx.o)
gcc -c A1.c A2.c
生成静态库文件libafile.a文件
ar crv libafile.a A1.o A2.o
使用.a 库文件,创建可执行程序
gcc -o test test.c libafile.a
./test
2、共享库.so 文件的生成与使用
生成目标文件(xxx.o)
gcc -c -fpic A1.c A2.c
生成共享库文件,so
gcc -shared*.o -o libsofile.so
使用.so 文件创建可执行文件
gcc -o test test.c libsofile.so
./test
出错了,运行 ldd test 查看链接情况
发现是找不到对应的.so文件,与上文所示情况一样,将.so文件复制到/usr/lib 目录下
sudo cp libsofile.so /usr/lib
再次执行
./test
三、示例
创建新目录:新建五个文件,main1.c
、sub1.c、sub1.h、sub2.c、sub2.h
代码如下:
main1.c
#include"sub1.h"
#include"sub2.h"
int main()
{
int x=2,y=9; //定义x和y变量
printf("%.2f\n",x2x(x,y)); //输出处理后的值,保留两位小数
printf("%f\n",x2y(x,y));
return 0;
}
sub1.c
#include "sub1.h"
float x2x(int a,int b)
{
float ans;
ans=(float)b/a;
return ans;
}
sub1.h
#ifndef __SUB1_H
#define __SUB1_H
#include<stdio.h>
float x2x(int a,int b);
#endif
sub2.c
#include "sub2.h"
double x2y(int a,int b)
{
double mul;
mul=(double)a*b;
return mul;
}
sub2.h
#ifndef __SUB2_H
#define __SUB2_H
#include<stdio.h>
double x2y(int a,int b);
#endif
1、使用.a静态库链接
使用gcc分别编译3个.o文件
gcc -c sub1.c sub2.c main1.c
使用ar工具生成一个.a静态库文件
ar crv libafile.a sub1.o sub2.o
使用gcc将main1函数的目标文件与此静态文件链接,生成最终的可执行文件
gcc -o main1 main1.c libafile.a
运行
./main1
记录可执行文件大小
size main1
由图可知,.a静态库生成的可执行文件txt大小为1661
2、.so 动态库链接生成可执行文件
将sub1、sub2目标文件用ar工具生成一个.so动态库文件
gcc -c -fpic sub1.c sub2.c
gcc -shared *.o -o libsofile.so
使用.so文件,创建可执行文件
gcc -o main2 main1.c libsofile.so
将.so 文件转移到/usr/lib 目录下
sudo cp libsoflie.so /usr/lib
执行:
./main2
查看可执行文件大小:
size main2
由图可知,main2可执行文件txt大小为1690
.a静态库文件与main函数链接生成的可执行文件 相比于 .so 动态库文件与main函数链接生成的可执行文件,动态库文件稍大一些,几乎差不多。