文章目录
1、概述
2、makefile
3、使用
4、问题处理
1、概述
通用的 c 程序 makefile。
2、makefile
#source file
ver=debug
# 源文件,自动找所有 .c 和 .cpp 文件,并将目标定义为同名 .o 文件
SOURCE := $(wildcard *.c) $(wildcard *.cpp)
OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))
#target you can change pp_monitor_server to what you want
# 目标文件名,输入任意你想要的执行文件名
TARGET := 程序名称
#compile and lib parameter
# 编译参数
CC := gcc
LIBS := -lpthread
LDFLAGS:=
DEFINES:=
INCLUDE:= -I.
CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H
ifeq ($(ver), debug)
CFLAGS = -g -Ddebug $(DEFINES) $(INCLUDE)
else
CFLAGS = -O3 $(DEFINES) $(INCLUDE)
endif
#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
rm -fr 程序名称
veryclean : clean
rm -fr $(TARGET)
$(TARGET) : $(OBJS)
$(CC) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)
3、使用
(1).h .c 文件与 makefile 同级目录;
(2)其他使用参见 makefile 注释;
(3)如果有依赖库可以添加到 LIB 中,例:LIBS := -lpthread
(3)编译:make;
(4)清除编译结果:make clean。
4、问题处理
(1)makefile:37: * missing separator (did you mean TAB instead of 8 spaces?). Stop.
使用时如果存在该问题,可能是不同编译器拷贝导致的,可以将对应的行前方的空格删掉,替换成 Tab 即可解决。