我最新的项目是用 C++ 编写的,我使用的是 GNU Make。
项目目录布局如下:
project
|-src
|-subdir1
|-subdir2 (containing tests)
|-doc
|-bin
我希望能够致电make
在顶级目录中(即项目目录中需要一个 makefile)来编译“src”子目录中的所有源代码并将生成的二进制文件放在“bin”目录中。
做到这一点的最佳方法是什么?如果我不必为每个源文件添加 make 规则,而只需为目录中的所有 .cc 和 .h 文件添加一个 make 规则,那就太好了。
Make 允许您概括您的规则,因此您无需为每个文件创建一个规则。
project
|-src
|-subdir1
|-subdir2 (containing tests)
|-doc
|-bin
你可以尝试这样的事情:
#This finds all your cc files and places then into SRC. It's equivalent would be
# SRC = src/main.cc src/folder1/func1.cc src/folder1/func2.cc src/folder2/func3.cc
SRC = $(shell find . -name *.cc)
#This tells Make that somewhere below, you are going to convert all your source into
#objects, so this is like:
# OBJ = src/main.o src/folder1/func1.o src/folder1/func2.o src/folder2/func3.o
OBJ = $(SRC:%.cc=%.o)
#Tells make your binary is called artifact_name_here and it should be in bin/
BIN = bin/artifact_name_here
# all is the target (you would run make all from the command line). 'all' is dependent
# on $(BIN)
all: $(BIN)
#$(BIN) is dependent on objects
$(BIN): $(OBJ)
g++ <link options etc>
#each object file is dependent on its source file, and whenever make needs to create
# an object file, to follow this rule:
%.o: %.cc
g++ -c $< -o $@
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)