我所使用的一个通用的Makefile模板

话不多说,请看:

我的项目有的目录结构有:

dirls/
├── include
│   └── apue.h
├── lib
│   ├── error.c
│   ├── error.o
│   └── Makefile
├── src
│   ├── dirls.c
│   ├── dirls.out
│   └── Makefile
└── test_client

而我的Makefile模板代码如下:

SRCS = $(wildcard *.c ../lib/*.c)    #wildcard把 指定目录 ./ 和 ../lib 下的所有后缀是c的文件全部展开。

OBJS = $(SRCS:.c = .o)    #OBJS将$(SRCS)下的.c文件转化为.o文件

CC = gcc   #代表所使用的编译器

INCLUDES = -I../include \   #头文件查找路径
           -I. \

LIBS = -L../lib \   #链接库查找地址

CCFLAGS = -g -Wall -O0   #附加参数

OUTPUT = dirls.out   #输出程序名称

all:$(OUTPUT)

$(OUTPUT) : $(OBJS)
    $(CC) $^ -o $@ $(INCLUDES) $(LIBS)

%.o : %.c
    $(CC) -c $< $(CCFLAGS)

clean:
    rm -rf *.out *.o    #清除中间文件及生成文件

.PHONY:clean

另外附上别的网站的几个Makefile模板:

1、编译动态库

############################################################# 
# Makefile for shared library.
# 编译动态链接库
#############################################################
#set your own environment option
CC = g++
CC_FLAG = -D_NOMNG -D_FILELINE

#set your inc and lib
INC = 
LIB = -lpthread -L./ -lsvrtool

#make target lib and relevant obj 
PRG = libsvrtool.so
OBJ = Log.o

#all target
all:$(PRG)

$(PRG):$(OBJ)
    $(CC) -shared -o $@ $(OBJ) $(LIB)

.SUFFIXES: .c .o .cpp
.cpp.o:
    $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o

.PRONY:clean
clean:
    @echo "Removing linked and compiled files......;
    rm -f $(OBJ) $(PRG)

2、编译静态库

#############################################################
# Makefile for static library.
# 编译静态链接库
#############################################################
#set your own environment option
CC = g++
CC_FLAG = -D_NOMNG -D_FILELINE

#static library use 'ar' command 
AR = ar

#set your inc and lib
INC = 
LIB = -lpthread -L./ -lsvrtool

#make target lib and relevant obj 
PRG = libsvrtool.a
OBJ = Log.o

#all target
all:$(PRG)
$(PRG):$(OBJ)
    ${AR} rv ${PRG} $?

.SUFFIXES: .c .o .cpp
.cpp.o:
    $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o

.PRONY:clean
clean:
    @echo "Removing linked and compiled files......"
    rm -f $(OBJ) $(PRG)

3、可执行程序

###########################################
#Makefile for simple programs
###########################################
INC=
LIB= -lpthread

CC=CC
CC_FLAG=-Wall

PRG=threadpooltest
OBJ=CThreadManage.o CThreadPool.o CThread.o CWorkerThread.o threadpooltest.o

$(PRG):$(OBJ)
    $(CC) $(INC) $(LIB) -o $@ $(OBJ)
    
.SUFFIXES: .c .o .cpp
.cpp.o:
    $(CC) $(CC_FLAG) $(INC) -c $*.cpp -o $*.o

.PRONY:clean
clean:
    @echo "Removing linked and compiled files......"
    rm -f $(OBJ) $(PRG)

 

posted @ 2014-10-19 10:52  中大黑熊  阅读(5655)  评论(1编辑  收藏  举报