Linux的Terminal中如何生成动态库以及如何使用动态库

来源:互联网 发布:恩威宝app软件下载 编辑:程序博客网 时间:2024/05/29 03:53

dll.c:

#include<stdio.h>void dll_init(){printf("dll_init ...\n");}

dll.h:

#ifndef __DLL_H#define __DLL_Hvoid dll_init();#endif

makefile:

.PHONY:cleanhello:main.o        gcc -o hello main.o -L./ -ldllmain.o:main.c        gcc -o $@ -c -fPIC $^clean:        rm -f main.o hello

指令执行:

root@ubuntu:~/lesson/chap2/2-6/2-6-2/test# makegcc -o hello main.o -L./ -ldllroot@ubuntu:~/lesson/chap2/2-6/2-6-2/test# lsdll.c  dll.h  hello  libdll.so  main.c  main.o  makefileroot@ubuntu:~/lesson/chap2/2-6/2-6-2/test# ./hello./hello: error while loading shared libraries: libdll.so: cannot open shared object file: No such file or directoryroot@ubuntu:~/lesson/chap2/2-6/2-6-2/test# cp libdll.so /usr/lib                 #把动态库存在库文件夹里面,才能运行./helloroot@ubuntu:~/lesson/chap2/2-6/2-6-2/test# ./hellohello wordld!dll_init ...root@ubuntu:~/lesson/chap2/2-6/2-6-2/test# 

注意: .代表当前文件夹.

阅读全文
0 0
原创粉丝点击