写一个makefile

muduo project在/home/pei/Desktop
目录结构大概是这样的,
muduo/base/BlockingQueue.h
muduo/base/test/BlockingQueue_test.cc
要把生成的obj文件放在muduo/base/test/obj/BlockingQueue_test.o
把生成的可执行文件放在muduo/base/test/bin/BlockingQueue_test

把makefile文件放在muduo/base/test/makefile
有点诡异是不是。。

DIR_INC = /home/pei/Desktop/ #../../../
DIR_SRC = ./
DIR_OBJ = ./obj/
DIR_BIN = ./bin/

INC = ${DIR_INC}muduo/base/BlockingQueue.h
SRC = ${DIR_SRC}BlockingQueue_test.cc  
OBJ = ${DIR_OBJ}BlockingQueue_test.o 
BIN = ${DIR_BIN}BlockingQueue_test

${BIN}: ${OBJ}
    g++ -pthread ${OBJ} -o ${BIN}

${OBJ}: ${SRC}
    g++ -c ${SRC} -pthread -o ${OBJ} -I ${DIR_INC}


.PHONY:clean
clean:
    rm ${BIN} ${OBJ} -fr
    #find ${DIR_OBJ} -name BlockingQueue_test.o -exec rm -rf {} \;

然后make
写一个makefile_第1张图片

执行一下生成的可执行文件
写一个makefile_第2张图片
还可以make clean一下
这里写图片描述
收工。

参考:
一个最简单的helloword
http://www.cnblogs.com/jacklikedogs/p/4125317.html

how to write makefile
http://seisman.info/how-to-write-makefile.html

-pthread和-pthread
http://blog.csdn.net/g446868263/article/details/6857840

g++ -I 等同于 vs中的包含目录

cmake practice
http://sewm.pku.edu.cn/src/paradise/reference/CMake%20Practice.pdf

notdir
dir1 = ${notdir ${SRC1}}
功能:去除所有的目录信息,SRC里的文件名列表将只有文件名。

patsubst
OBJ1 = ${DIR_OBJ}${patsubst %.cc,%.o,${dir1}}
功能:patsubst是patten substitude的缩写,匹配替代的意思。这句是在dir1中找到所有.cc 结尾的文件,然后把所有的.cc换成.o。

DIR_INC = /home/pei/Desktop/ #../../../
DIR_SRC = ./
DIR_OBJ = ./obj/
DIR_BIN = ./bin/

#INC1 = ${DIR_INC}muduo/base/BlockingQueue.h 
SRC1 = ${DIR_SRC}BlockingQueue_test.cc 
dir1 = ${notdir ${SRC1}}
OBJ1 = ${DIR_OBJ}${patsubst %.cc,%.o,${dir1}}
BIN1 = ${DIR_BIN}BlockingQueue_test

#INC2 = ${DIR_INC}muduo/base/BoundedBlockingQueue.h 
SRC2 = ${DIR_SRC}BoundedBlockingQueue_test.cc 
dir2 = ${notdir ${SRC2}}
OBJ2 = ${DIR_OBJ}${patsubst %.cc,%.o,${dir2}}
BIN2 = ${DIR_BIN}BoundedBlockingQueue_test

TARGET = ${BIN1} ${BIN2}
all : ${TARGET}

${BIN1}: ${OBJ1}
    g++ -pthread ${OBJ1} -o ${BIN1}
${OBJ1}: ${SRC1}
    g++ -c ${SRC1} -pthread -o ${OBJ1} -I ${DIR_INC}
${BIN2}: ${OBJ2}
    g++ -pthread ${OBJ2} -o ${BIN2}
${OBJ2}: ${SRC2}
    g++ -c ${SRC2} -pthread -o ${OBJ2} -I ${DIR_INC}


.PHONY:clean
clean:
    find ${DIR_OBJ} -name *.o -exec rm -rf {} \;
    find ${DIR_BIN} -type f | xargs rm -fr

题外话:

有时候真的好气,但却不由自主的微笑。

你可能感兴趣的:(C++)