为什么我的makefile特定目标变量的工作? [英] Why doesn't my makefile target-specific variable work?

查看:183
本文介绍了为什么我的makefile特定目标变量的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个makefile其中执行以下操作:

I am trying to make a makefile which does the following:


  • 的src 目录
  • 获取源文件
  • 把目标文件在 OBJ 目录

  • 把二进制文件在目录

  • 把目标放在相对目录

  • 使调试目标在 DBG 目录

  • gets source files from the src directory
  • puts object files in the obj directory
  • puts binary files in the bin directory
  • puts release targets in the rel directory
  • puts debug targets in the dbg directory

我遇到的第一个问题是,我的目标特定的变量似乎并没有在这里工作的是生成文件:

The first problem i'm having is that my target-specific variables don't seem to be working here is the makefile:

# Build Directories
src_dir=src
obj_dir=obj
bin_dir=bin

cc=cl
cc_flags=

# create the directory for the current target.
dir_guard=@mkdir -p $(@D)

# source files
src = MainTest.cpp

# object files - replace .cpp on source files with .o and add the temp directory prefix
obj = $(addprefix $(obj_dir)/$(cfg_dir)/, $(addsuffix .obj, $(basename $(src))))

release: cfg_dir = rel
release: executable

debug: cfg_dir = dbg
debug: cc_flags += -Yd -ZI
debug: executable

executable: $(bin_dir)/$(cfg_dir)/MainTest.exe

# build TwoDee.exe from all of the object files.
$(bin_dir)/$(cfg_dir)/MainTest.exe : $(obj)
    $(dir_guard)
    $(cc) -out:$@ $(obj) -link

# build all of the object files in the temp directory from their corresponding cpp files.
$(obj_dir)/$(cfg_dir)/%.obj : $(source_dir)/%.cpp
    $(dir_guard)
    $(cc) $(cc_flags) -Fo$(obj_dir)/$(cfg_dir) -c $<

当我运行make调试,我得到:

When I run make debug I get:

make: *** No rule to make target `obj//MainTest.obj', needed by `bin//MainTest.exe'.

还有别的东西错了,因为如果我删除了调试和发布变量和硬code cfg_dir为rel 然后我得到:

make: *** No rule to make target `obj/rel/MainTest.obj', needed by `bin/rel/MainTest.exe'.  Stop.

所以,我的目标规则也一定是错误的。我是新做的文件,所以如果有人看到其他的事情是错误的,评论是值得欢迎的。

So my object rule must also be wrong. I am new to make files so if anyone sees other things which are wrong, comments are welcome.

推荐答案

特定目标变量是仅在食谱提供,而不是在规则。这是因为当读出生成文件的规则被解析,和依赖由该推断

Target-specific variables are only available in the recipes, not in the rules. This is because the rules are parsed when the Makefile is read, and dependencies are deduced from that.

要获得 cfg_dir 的多个可能值定制规则,我建议你看看在的评估的GNU的部分进行手动。这就解释了这样的事情:

To get your rules tailored for multiple possible values of cfg_dir, I suggest you look at the example in the eval section of the GNU Make manual. This explains things like this:

release: $(bin_dir)/rel/MainTest.exe

debug: cc_flags += -Yd -ZI
debug: $(bin_dir)/dbg/MainTest.exe

define template =

# build TwoDee.exe from all of the object files.
$(bin_dir)/$(cfg_dir)/MainTest.exe : $(obj)
    $$(dir_guard)
    $$(cc) -out:$$@ $(obj) -link

# build all of the object files in the temp directory from their corresponding cpp files.
$(obj): $(obj_dir)/$(cfg_dir)/%.obj : $$(src_dir)/%.cpp
    $$(dir_guard)
    $$(cc) $$(cc_flags) -Fo$(obj_dir)/$(cfg_dir) -c $$<

endef
$(foreach cfg_dir,rel dbg,$(eval $(template)))

这篇关于为什么我的makefile特定目标变量的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆