【发布时间】:2017-07-06 06:43:01
【问题描述】:
我的 C++ 项目的目录结构如下:
.
├── bin
├── build
├── include
│ ├── dir1
│ │ ├── file1.hpp
│ │ └── file2.hpp
│ ├── dir2
│ │ ├── file3.hpp
│ │ └── file4.hpp
│ └── third_party
│ └── catch.hpp
├── Makefile
├── src
│ ├── dir1
│ │ ├── file1.cpp
│ │ └── file2.cpp
│ └── dir2
│ ├── file3.cpp
│ └── file4.cpp
└── test
├── dir1
│ ├── file1.test.cpp
│ └── file2.test.cpp
└── dir2
├── file3.test.cpp
└── file4.test.cpp
如何编写 Makefile 来编译 src 和 test 目录中的代码并获取 build 目录中的目标文件和 bin 目录中的二进制文件,并在其中保持相同的目录结构, 无需明确命名每个文件及其依赖项?在src 和test 中的每个dir* 中使用多个Makefile 会更好吗?
我的 Makefile 目前看起来像这样:(这可能是荒谬的,对此感到抱歉!)
binaries_dir = bin
build_dir = build
sources_dir = src
include_dir = include
compile_flags = -std=c++14 -Wall
binaries := $(wildcard *.out)
objects := $(wildcard *.o)
sources := $(wildcard *.cpp)
headers := $(wildcard *.hpp)
objects: $(sources)
g++ $(compile_flags) -c $(sources_dir)/$(sources) -I $(include_dir)
binaries: $(objects)
for object in $(objects); do
g++ $(compile_flags) -o $(binaries_dir)/ $(build_dir)/$object
done
【问题讨论】:
-
我在latedev.wordpress.com/2014/11/08/… 有几篇(不完整的)文章,介绍了如何编写通用的 makefile,这可能有用也可能没用。
-
@NeilButterworth 这似乎很有用,谢谢!通过 ATM。
-
这真的取决于你的目标程序是什么。这是否都构建了一个程序?还是每个源文件一个程序?
-
@Galik 每个源文件一个程序
-
假设文件名将是唯一的,以便在
bin目录中的程序不会相互覆盖?或者您想要源目录中bin目录下的相同子目录?