问题描述
我希望能够将bin和我的代码文件拆分到单独的目录中,因为在当前状态下它变得越来越难以管理.
I want to be able to split up my bin and my code files into separate directories as it is becoming hard to manage in it's current state.
我理想地希望拥有
project_dir
|-Makefile
|-run_tests.sh
|
|__source
| |-program1.cpp
| |-program2.cpp
|
|__bin
|-program1
|-program2
但是,如果不为每个程序手动编写规则,我将无法使它与当前系统配合使用(请记住,每个程序都是一个单独的程序,而不是一系列链接在一起的对象)
However I am unable to get this to work with my current system without having to manually write out the rules for every program (bear in mind that every program is a separate program, not a series of objects linked together)
#Current make system
BIN=./bin/
SOURCE=./source/
LIST=program1 program2...
all: $(LIST)
%: $(SOURCE)%.cpp
$(CC) $(INC) $< $(CFLAGS) -o $(BIN)$@ $(LIBS)
除了它在当前路径中看不到目标之外,它可以工作,即使它没有更改源文件,它也认为它始终会重建二进制文件.
this works except it I it can't see the target in the current path so it think it always rebuilds the binaries even if the source files haven't changed.
此刻我唯一的想法是编写一个程序来制作一个makefile,但我不想这么做.
My only thought at the moment is to write a program to make a makefile but I don't want to do that.
推荐答案
您快到了...
#Current make system
BIN=./bin/
SOURCE=./source/
LIST=$(BIN)/program1 $(BIN)/program2...
all: $(LIST)
$(BIN)/%: $(SOURCE)%.cpp
$(CC) $(INC) $< $(CFLAGS) -o $@ $(LIBS)
您还可以使用以下内容使LIST
变得更容易
You can also make the LIST
easier by using the following
PROG=program1 program2
LIST=$(addprefix $(BIN)/, $(PROG))
这篇关于Makefile如何将单独的目录用于源代码和二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!