dll.c:
#include<stdio.h> void dll_init() { printf("dll_init ...\n"); }dll.h:
#ifndef __DLL_H #define __DLL_H void dll_init(); #endifmakefile:
.PHONY:clean hello:main.o gcc -o hello main.o -L./ -ldll main.o:main.c gcc -o $@ -c -fPIC $^ clean: rm -f main.o hello指令执行:
root@ubuntu:~/lesson/chap2/2-6/2-6-2/test# make gcc -o hello main.o -L./ -ldll root@ubuntu:~/lesson/chap2/2-6/2-6-2/test# ls dll.c dll.h hello libdll.so main.c main.o makefile root@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 directory root@ubuntu:~/lesson/chap2/2-6/2-6-2/test# cp libdll.so /usr/lib #把动态库存在库文件夹里面,才能运行./hello root@ubuntu:~/lesson/chap2/2-6/2-6-2/test# ./hello hello wordld! dll_init ... root@ubuntu:~/lesson/chap2/2-6/2-6-2/test#注意: .代表当前文件夹.