使用不带foo =的makefile参数 [英] Using makefile arguments without foo=
问题描述
我有一个makefile,可用来编译单个文件.当我需要传递参数时,我使用target = targetFile.
I have a makefile I use to compile a single file. When I need to pass an argument, I use target=targetFile.
脚本将使用参数,在与该参数具有相同值的文件(位于同一目录中)中进行查找并进行编译.
The script takes the argument, looks for the file (within the same directory) that has the same value as the argument and compiles it.
我用它来编译来自使用单个C ++文件的uhunt和uva的问题.所以我不需要多个源文件的多个makefile.多个源文件的单个makefile是我制作makefile的原因.
I use this for compiling problems from uhunt and uva, which use a single c++ file. So I dont' need multiple makefiles for multiple source files. Single makefile for multiple source files is the reason I made the makefile.
这是我到目前为止的代码
Here's the code I have so far
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
The command I use to compile is,
make target=sourceFile
我也不包括文件扩展名,我所有的源文件扩展名都为cpp
Also I don't include the file extension, I have all my source file extensions to be cpp
我最后想要的是:
make sourceFile
请注意,为了使用命令clean and clean-all,我使用
Just a side note, for using the command clean and clean-all, I use
make target=sourceFile clean
make target=sourceFile clean-all
我希望可以使用:
make sourceFile clean
make sourceFile clean-all
推荐答案
您可以使用通用的Makefile变量MAKECMDGOALS
,该变量包含传递给make的所有目标.
You may use common Makefile variable MAKECMDGOALS
that contains all targets passed to make.
请尝试使用此变体
CC = g++
CFLAGS = -Wall -g
MAKECMDGOALS := $(filter-out clean, $(MAKECMDGOALS))
.PHONY: $(MAKECMDGOALS)
$(MAKECMDGOALS):
$(CC) $(CFLAGS) $@.c -o Main-$@
clean:
rm -f *.o
在这里
$(MAKECMDGOALS):
$(CC) $(CFLAGS) $@.c -o Main-$@
将为MAKECMDGOALS
中的每个单词生成单独的构建目标.
will generate separate build targets for each word in MAKECMDGOALS
.
注意,我们需要这个Makefile来知道'clean'是删除内容的目标,而不是尝试构建Main-clean
.这就是为什么我们使用过滤器功能从MAKECMDGOALS
中删除clean
的原因.
Note, we need this Makefile to know that 'clean' is a target for removing stuff, but not to attempt build Main-clean
. This why we remove clean
from MAKECMDGOALS
using filter-out function.
因此,如果我们运行make a b clean
,构建系统将自动生成用于构建Main-a
和Main-b
的目标,然后使用已编写的clean
目标
So if we run make a b clean
, the build system will generate automatically targets for building Main-a
and Main-b
and then use already written clean
target
这篇关于使用不带foo =的makefile参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!