makefile生成静/动态库
通过makefile生成静态库和动态库
目录树
➜ app_hello tree -h
.
├── [ 280] app_hello.c
├── [ 218] app_hello.h
└── [ 997] makefile
0 directories, 3 files
makefile
ROOT_DIR := $(shell pwd)
SRC := $(wildcard *.c)
OBJ := $(patsubst %.c,%.o,$(SRC))
export DFLAGS ?= # 库的链接参数
export CFLAGS ?= # 参数信息
export CROSS_COMPILE ?= # 没有定义交叉编译器时,直接用gcc
export CC := $(CROSS_COMPILE)gcc # 编译器 按需就行
export AR = ${CROSS_COMPILE}ar
export STRIP = ${CROSS_COMPILE}strip
export AS = ${CROSS_COMPILE}as
export LD = ${CROSS_COMPILE}lr
export RANLIB = ${CROSS_COMPILE}ranlib
export NM = ${CROSS_COMPILE}nm
LIB_SO_NAME := libhello.so
LIB_REAL_NAME := libhello.so.1
LIB_A_NAME :=libhello.a
.PHONY :all clean LIB_SO LIB_A
all: LIB_SO LIB_A
LIB_A : $(OBJ)
$(AR) r $(LIB_A_NAME) $(OBJ)
LIB_SO : $(OBJ)
$(info $(SRC) $(OBJ))
$(CC) $(OBJ) -shared -Wl,-soname,$(LIB_SO_NAME) -o $(LIB_REAL_NAME)
%.o : %.c
$(CC) -fPIC -c $< -o $@ $(CFLAGS) $(DFLAGS)
clean:
rm -rf $(OBJ)
rm -rf $(LIB_REAL_NAME)
rm -rf $(LIB_A_NAME)
测试
➜ app_hello make clean all
rm -rf app_hello.o
rm -rf libhello.so.1
rm -rf libhello.a
gcc -fPIC -c app_hello.c -o app_hello.o
app_hello.c app_hello.o
gcc app_hello.o -shared -Wl,-soname,libhello.so -o libhello.so.1
ar r libhello.a app_hello.o
ar: creating libhello.a
➜ app_hello nm -D libhello.so.1
w _ITM_deregisterTMCloneTable
w _ITM_registerTMCloneTable
w __cxa_finalize
w __gmon_start__
0000000000001119 T app_hello
U puts
➜ app_hello nm libhello.a
app_hello.o:
U _GLOBAL_OFFSET_TABLE_
0000000000000000 T app_hello
U puts