gpt4 book ai didi

build - 为什么我的 makefile 特定于目标的变量不起作用?

转载 作者:行者123 更新时间:2023-12-04 05:40:22 25 4
gpt4 key购买 nike

我正在尝试制作一个执行以下操作的 makefile:

  • 获取源文件源 目录
  • 将目标文件放入 物镜目录
  • 将二进制文件放入 垃圾箱目录
  • 将发布目标放在 中rel 目录
  • 将调试目标放在 中dbg 目录

  • 我遇到的第一个问题是我的特定于目标的变量似乎在这里不起作用是 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 debug 时,我得到:
    make: *** No rule to make target `obj//MainTest.obj', needed by `bin//MainTest.exe'.

    还有其他错误,因为如果我删除调试和发布变量和硬编码 cfg_dir 到 rel 然后我得到:
    make: *** No rule to make target `obj/rel/MainTest.obj', needed by `bin/rel/MainTest.exe'.  Stop.

    所以我的对象规则也一定是错误的。我是制作文件的新手,所以如果有人看到其他错误的东西,欢迎评论。

    最佳答案

    特定于目标的变量仅在配方中可用,在规则中不可用。这是因为在读取 Makefile 时会解析规则,并从中推断出依赖关系。

    cfg_dir 的多个可能值定制您的规则,建议你看eval中的例子GNU Make 手册的部分。这解释了这样的事情:

    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)))

    关于build - 为什么我的 makefile 特定于目标的变量不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11331390/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com