Makefile常⽤万能模板
1、⽣成可执⾏⽂件的makefile
>>>>>>>###
#
>>>>>>>###
#source file
#源⽂件,⾃动所有.c和.cpp⽂件,并将⽬标定义为同名.o⽂件
SOURCE  :=$(wildcard *.c)$(wildcard *.cpp)
OBJS    :=$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
#target you can change test to what you want
#⽬标⽂件名,输⼊任意你想要的执⾏⽂件名
TARGET  :=test
#compile and lib parameter
#编译参数
CC      := gcc
makefile phonyLIBS    := -L/usr/local/lib/ -lev #$(wildcard ./libmbedtls/*.a)
LDFLAGS := lpthread -lm -levent    #-lm 这将告诉gcc链接您的代码与数学库。只要确保把标志放在你想链接的对象后⾯DEFINES :=
INCLUDE := -I. -I./json/
CFLAGS  := -g -Wall -O3 $(DEFINES)$(INCLUDE) -static
CXXFLAGS:=$(CFLAGS) -DHAVE_CONFIG_H
#i think you should do anything here
#下⾯的基本上不需要做任何改动了
.PHONY : everything objs clean veryclean rebuild
everything :$(TARGET)
all :$(TARGET)
objs :$(OBJS)
rebuild: veryclean everything
clean :
rm -fr *.so
rm -fr *.o
veryclean : clean
rm -fr $(TARGET)
$(TARGET):$(OBJS)
$(CC)$(CXXFLAGS) -o $@$(OBJS)$(LDFLAGS)$(LIBS)
2、⽣成静态链接库的makefile
#target you can change test to what you want
#共享库⽂件名,lib*.a
TARGET  := libtest.a
#compile and lib parameter
#编译参数
CC      := gcc
AR      = ar
RANLIB  = ranlib
LIBS    :=
LDFLAGS :=
DEFINES :=
INCLUDE := -I.
CFLAGS  := -g -Wall -O3 $(DEFINES)$(INCLUDE) CXXFLAGS:=$(CFLAGS) -DHAVE_CONFIG_H
#i think you should do anything here
#下⾯的基本上不需要做任何改动了
#source file
#源⽂件,⾃动所有.c和.cpp⽂件,并将⽬标定义为同名.o⽂件SOURCE  :=$(wildcard *.c)$(wildcard *.cpp)
OBJS    :=$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
.PHONY : everything objs clean veryclean rebuild
everything :$(TARGET)
all :$(TARGET)
objs :$(OBJS)
rebuild: veryclean everything
clean :
rm -fr *.o
veryclean : clean
rm -fr $(TARGET)
$(TARGET):$(OBJS)
$(AR) cru $(TARGET)$(OBJS)
$(RANLIB)$(TARGET)
3、⽣成动态链接库的makefile
#target you can change test to what you want
#共享库⽂件名,lib*.so
TARGET  := libtest.so
#compile and lib parameter
#编译参数
CC      := gcc
LIBS    :=
LDFLAGS :=
DEFINES :=
INCLUDE := -I.
CFLAGS  := -g -Wall -O3 $(DEFINES)$(INCLUDE) CXXFLAGS:=$(CFLAGS) -DHAVE_CONFIG_H
SHARE  := -fPIC -shared -o
#i think you should do anything here
#下⾯的基本上不需要做任何改动了
#source file
#源⽂件,⾃动所有.c和.cpp⽂件,并将⽬标定义为同名.o⽂件SOURCE  :=$(wildcard *.c)$(wildcard *.cpp)
OBJS    :=$(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) .PHONY : everything objs clean veryclean rebuild
everything :$(TARGET)
all :$(TARGET)
objs :$(OBJS)
rebuild: veryclean everything
clean :
rm -fr *.o
veryclean : clean
rm -fr $(TARGET)
$(TARGET):$(OBJS)
$(CC)$(CXXFLAGS)$(SHARE)$@$(OBJS)$(LDFLAGS)$(LIBS)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。

发表评论