makefile入门小trick
为了学C socket编程,最近安装了ubuntu虚拟机,然后由于安不了VS这种的IDE,所以本鼠只好学怎么写makefile。教程看的C语言中文网教程:http://c.biancheng.net/view/7153.html。
虚拟机上代码的目录结构为:
由于makefile文件与源文件不在同一目录下,所以需要使用vpath来指定从哪里找源文件。同时,不要在依赖规则中带入路径。g++ -c,g++ -o编译得到得到的.o文件与可执行文件与makefile文件在同一目录下,要使用mv命令移动到bin文件夹中。
以下为makefile代码:
#declare varibles(like macro definition)
INCL=/home/zhg/文档/net_program/mf_test/incl
SRC=/home/zhg/文档/net_program/mf_test/src
BIN=/home/zhg/文档/net_program/mf_test/bin
HOME=/home/zhg/文档/net_program/mf_test
vpath %.cpp src
vpath %.h incl
#rules
.SUFFIXES:.cpp
.cpp.o:
g++ ${SRC} -c $<
#compile
all:man.o test.o
g++ -o test $^
mv test ${BIN}
rm -f $^
man.o:man.cpp
test.o:test.cpp
后续有新的知识再记录。