1. 生成静态库和动态库
文件:makefile test.cpp test.h
lib := $(PWD)/../../libs/libtest.a
so := $(PWD)/../../libs/libtest.so
all : ${lib} ${so}
${lib} : test.o
ar rcs ${lib} test.o
${so} : test.o
gcc -shared -fPIC -o ${so} test.o
test.o : test.cpp test.h
gcc -c test.cpp -o test.o
2. 调用静态库或动态库
HEAR := -I include
LIB := -L libs
obj = main.o
main : $(obj)
gcc ${obj} -o main ${LIB} -ltest
main.o : main.cpp include/zhy/test.h
gcc -c main.cpp -o main.o ${HEAR}
clean:
rm -rf main main.o
注:动态库需要 export LD_LIBRARY_PATH=./libs/
3.进阶
Makefile教程(绝对经典,所有问题看这一篇足够了)_GUYUEZHICHENG的博客-CSDN博客_makefile
参考:
linux编程入门(六)-编写Makefile文件 - 简书
Linux基础——gcc编译、静态库与动态库(共享库)_daidaihema的博客-CSDN博客_gcc 动态库编译