我所使用的一個通用的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)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM