如何使用Makefile变量作为要包含在g ++命令中的文件? [英] How to use Makefile variables as files to include in g++ command?
问题描述
这是我的问题:我有一个makefile文件,其中包含用于编译项目中所有内容的信息,以使我的生活更轻松,但是最近这让我有些头疼.
Here's my problem : I have a makefile file containing the info to compile everything in my project to make my life easier, but it is giving me some headaches recently.
它可以编译多个文件,例如:
It can compile multiple files, such as this :
objects/io.o: sources/io.cpp
@g++ -c $< -o $@ -std=c++11
objects/map.o: sources/map.cpp
@g++ -c $< -o $@ -std=c++11
在makefile的顶部,我有这样声明的变量:
At the top of the makefile, I have variables declared as such :
IO="objects/io.o"
MAP="objects/map.o"
[... other object files ...]
ALL="$(IO) $(MAP) [...]"
当我要编译主文件时,我使用以下命令:
When I want to compile my main file, I use this command :
main.exe: tests/main.cpp
@g++ $< $(ALL) -o $@ -std=c++11
当我手动编译此问题时(在命令的一行中输入所有内容,而不是使用make main.exe
进行编译),则编译时不会出现问题.
When I compile this problem manually (inputting everything in one line of command, instead of making make main.exe
) it compiles without problems.
但是,当我在项目中使用make命令时,会弹出以下错误:
However, when I use the make command for the project, the following error pops up :
clang: error: no such file or directory: 'objects/io.o objects/map.o [...]'
make: *** [main.exe] Error 1
我不能以这种方式使用变量吗?我非常困惑.我知道这些文件都是经过编译的,似乎make
实用工具无法理解文件路径.
Can I not use variables this way ? I'm extremely confused. I know for a fact those files are compiled, it just seems the make
utility doesn't understand file paths.
推荐答案
我认为问题在于您引用了变量.尝试取消引号,它们将不会扩展单个参数:
I think the problem is that you have quoted your variables. Try unquoting them and they won't expand a single parameter:
ALL = $(obj1) $(obj2) ...
此外,对于那些使用相同编译过程的对象,我通常会定义一条规则:
Also, for those objects which use the same compilation process I generally define a single rule:
obj/%.o: %.cc
$(GCC) -o $@ -c $< $(FLAGS)
显然,这需要定义额外的变量GCC
和FLAGS
Obviously that would require defining the extra variables GCC
and FLAGS
这篇关于如何使用Makefile变量作为要包含在g ++命令中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!