使用源子目录制作文件 [英] make file with source subdirectories

查看:86
本文介绍了使用源子目录制作文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



来自Scala背景,我习惯了诸如sbt或ant之类的工具来自动化我的构建过程。我最新的项目是C ++,我被限制使用GNU Make。
项目目录布局如下:


coming from a Scala background, I am used to tools such as sbt or ant to automate my building processes. My latest project is in C++ and I am constrained to using GNU Make. The project directory layout is as follows:

project
|-src
  |-subdir1
  |-subdir2 (containing tests)
|-doc
|-bin

我想能够在顶级目录中调用 make (即在项目目录中需要一个makefile)来编译src中的所有源子目录,并将生成的二进制文件放在bin目录中。

最好的做法是什么?如果我不需要为每个源文件添加一个make规则,但是目录中只有一个用于.cc和.h文件,那样也会很好。

I would like to be able to call make in the top level directory (i.e. need a makefile in the project directory) to compile all sources in both "src" subdirectories and place the resulting binaries in the "bin" directory.
What would be the best approach to do this? It would also be great if I didn't have to add a make rule for every source file, but just had one for all .cc and .h files in the directories.

谢谢

--jakob

Thanks
--jakob

推荐答案

Make允许你概括你的规则,所以你赢了'需要为每个文件创建一个。

Make allows you to generalise your rules, so you won't need to create one for every file.

project
|-src
  |-subdir1
  |-subdir2 (containing tests)
|-doc
|-bin

你可以尝试这样的事情:

You can try something like this:

#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 $@

这篇关于使用源子目录制作文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆