【发布时间】:2016-10-28 04:53:47
【问题描述】:
我有一个用于编译单个文件的 makefile。当我需要传递参数时,我使用 target=targetFile。
脚本接受参数,查找与参数具有相同值的文件(在同一目录中)并编译它。
我用它来编译来自 uhunt 和 uva 的问题,它们使用单个 c++ 文件。所以我不需要多个源文件的多个makefile。多个源文件的单个 makefile 是我制作 makefile 的原因。
这是我目前的代码
OBJS = $(target).o
CC = g++
CFLAGS = -Wall -g -std=c++11
INCLUDE = -I./$(target)
#default command to run
all : Main-$(target) clean run
#compile and build
Main-$(target) : $(OBJS)
$(CC) $(CFLAGS) $^ -o $@
%.o : %.cpp
$(CC) -c $(CFLAGS) $<
#remove object and any other garbage files.
clean:
rm -rf -d $(target).o *~ *% *# .#*
#remove the compiled file
clean-all:
$(clean) rm Main-$(target)
#run the compiled file
run:
./Main-$(target)
我用来编译的命令是,
make target=sourceFile
另外我不包括文件扩展名,我的所有源文件扩展名都是 cpp
我最终想要的是:
make sourceFile
顺便说一句,为了使用命令 clean 和 clean-all,我使用
make target=sourceFile clean
make target=sourceFile clean-all
如果我可以使用,我会更喜欢:
make sourceFile clean
make sourceFile clean-all
【问题讨论】:
-
也许将通用部分移动到您可以包含的模板中,并拥有一个仅提供您想要的特定目标的狭窄主 makefile。