Makefile编译库

时间:2022-09-14 14:31:58

funs.h:

#ifndef __FUNS_H__
#define __FUNS_H__ void fun1(); #endif

funs.c

#include "funs.h"
#include <stdio.h> void fun1()
{
printf("this is fun1");
}

Makefile

CC = gcc
GXX = g++
LD = ld
AR = ar CFLAGS = -g -O2 -Wall -fpic
LDFLAGS = -shared -fpic
ARFLAGS = -rc OBJS = funs.o LIBNAME = libfuns
SHARE_LIB = $(LIBNAME).so
STATIC_LIB = $(LIBNAME).a .PHONY : all clean all : clean $(SHARE_LIB) $(STATIC_LIB) $(STATIC_LIB) : $(OBJS)
$(AR) $(ARFLAGS) -o $(STATIC_LIB) $(OBJS) $(SHARE_LIB) : $(OBJS)
$(LD) $(LDFLAGS) -o $(SHARE_LIB) $(OBJS) %.o : %.c
$(CC) $(CFLAGS) -c -o $@ $< clean:
rm -f $(OBJS) $(SHARE_LIB) $(STATIC_LIB)

main.c

#include "funs.h"

int main()
{
fun1();
}

编译:

gcc -o main.exe -lfuns -L/home/xt/test_lib main.c