我的最新项目是用C++编写的,我使用的是GNU Make.项目目录布局如下:
project
|-src
|-subdir1
|-subdir2 (containing tests)
|-doc
|-bin
Run Code Online (Sandbox Code Playgroud)
我希望能够调用make
顶级目录(即需要项目目录中的makefile)来编译"src"子目录中的所有源代码,并将生成的二进制文件放在"bin"目录中.
这样做的最佳方法是什么?如果我不必为每个源文件添加一个make规则,但是只需要为目录中的所有.cc和.h文件添加一个make规则,这也会很棒.
Sag*_*gar 14
Make允许您概括规则,因此您不需要为每个文件创建一个规则.
project
|-src
|-subdir1
|-subdir2 (containing tests)
|-doc
|-bin
Run Code Online (Sandbox Code Playgroud)
你可以尝试这样的事情:
#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 $@
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11054 次 |
最近记录: |