静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库。
动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入,因此在程序运行时还需要动态库存在。
查看当前工程目录的内容
[root@root config-file]# tree
.
|-- Makefile
|-- Makefile.a
|-- Makefile.so
|-- configfile.c
|-- configfile.o
|-- include
| |-- configfile.h
| `-- our_syslog.h
|-- main.c
`-- main.o
1 directory, 9 files
[root@root config-file]# cat include/*.h
++++++++++++++++++++++++++++++++++++++++++++
int read_config_file(char *filename, char *keyword, char *value);
#define dmsg(log,format,args...) do{if(log) \
fprintf(stderr, "WARING: " format , ##args); \
else \
fprintf(stderr, "INFO: " format , ##args); \
}while(0)
++++++++++++++++++++++++++++++++++++++++++++
Makefile.so生成动态链接库的makefile文件
[root@root config-file]# cat Makefile.so
CFLAGS += -I/root/config-file/include -fPIC
DFLAGS += $(CFLAGS) -shared
all : libfile.so
libfile.so: configfile.o
$(CC) $^ -o $@ $(DFLAGS)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY : clean
clean :
-rm -f libfile.so *.o
Makefile.a生成静态链接库的makefile文件
[root@root config-file]# cat Makefile.a
CFLAGS += -I/root/config-file/include
all : libfile.a
libfile.a: configfile.o
$(AR) rcs $@ $^
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
.PHONY : clean
clean :
-rm -f libfile.a *.o
主程序
[root@root config-file]# cat main.c
#include
#include
#include "configfile.h"
#include "our_syslog.h"
int main()
{
char value[100]={0};
read_config_file("/root/syslog.conf", "openwrt", value);
dmsg(0,"%s\n", value);
dmsg(1,"%s\n", value);
return 0;
}
主程序的Makefile
[root@root config-file]# cat Makefile
OBJS=configfile.o main.o
CFLAGS=-I/root/config-file/include
CFLIBS=-L/root/config-file -lfile -g -O -Wall
all:test
test: $(OBJS)
$(CC) $^ -o $@ $(CFLAGS) $(CFLIBS)
%.o:%.c
$(CC) -c $< -o $@ $(CFLAGS)
.PHONY : clean
clean :
-rm -f test *.o
生产静态库
[root@root config-file]# make -f Makefile.a
gcc -I/root/config-file/include -c configfile.c -o configfile.o
ar rcs libfile.a configfile.o
[root@root config-file]# ar -t libfile.a #查看静态库中的文件
configfile.o
连接静态库,编译主程序
[root@root config-file]# make
gcc -c configfile.c -o configfile.o -I/root/config-file/include
gcc -c main.c -o main.o -I/root/config-file/include
gcc configfile.o main.o -o test -I/root/config-file/include -L/root/config-file -lfile -g -O -Wall
[root@root config-file]# ./test
INFO: hello-openwrt
WARING: hello-openwrt
[root@root config-file]# rm libfile.a
rm:是否删除 一般文件 “libfile.a”? y
生产动态库
[root@root config-file]# make -f Makefile.so
gcc -I/root/config-file/include -fPIC -c configfile.c -o configfile.o
gcc configfile.o -o libfile.so -I/root/config-file/include -fPIC -shared
[root@root config-file]# ls
configfile.c configfile.o include libfile.so main.c Makefile Makefile.a Makefile.so
连接动态库,编译主程序
[root@root config-file]# make
gcc -c configfile.c -o configfile.o -I/root/config-file/include
gcc -c main.c -o main.o -I/root/config-file/include
gcc configfile.o main.o -o test -I/root/config-file/include -L/root/config-file -lfile -g -O -Wall
[root@root config-file]# ./test
./test: error while loading shared libraries: libfile.so: cannot open shared object file: No such file or directory
这就是动态库和静态库的区别了
[root@root config-file]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/config-file
也可以放到/usr/lib下(64位系统/usr/lib64)
[root@root config-file]# ./test
INFO: hello-openwrt
WARING: hello-openwrt
使用ldd命令查看可执行文件依赖于哪些库
[root@root config-file]# ldd test
linux-gate.so.1 => (0x00782000)
libfile.so (0x007ae000)
libc.so.6 => /lib/libc.so.6 (0x007f4000)
/lib/ld-linux.so.2 (0x007d1000)