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

xiaoxiao2021-02-28  94

dll.c:

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

dll.h:

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

makefile:

.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#

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

转载请注明原文地址: https://www.6miu.com/read-45463.html

最新回复(0)