You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Premake5未创建项目必要输出目录,该如何解决?

Premake5 + gmake2 构建时 mkdir 目录不存在的解决办法

问题场景

Windows系统下使用Premake5作为C项目构建系统,项目包含三个Premake脚本:

  1. 根目录 premake5.lua
workspace "epoch"
    configurations { "Debug", "Release", "Dist" }
    architecture "x86_64"
    startproject "sandbox"

include "runtime/runtime.lua"
include "sandbox/sandbox.lua"
  1. runtime/runtime.lua
project "runtime"
    kind "SharedLib"
    language "C"

    targetdir "../bin/%{cfg.buildcfg}"
    objdir "../obj/%{cfg.buildcfg}"

    files {
        "runtime/src/**.c",
        "runtime/src/**.h"
    }

    filter "configurations:Debug"
        symbols "On"

    filter "configurations:Release or configurations:Dist"
        optimize "On"
  1. sandbox/sandbox.lua
project "sandbox"
    kind "WindowedApp"
    language "C"

    targetdir "../bin/%{cfg.buildcfg}"
    objdir "../obj/%{cfg.buildcfg}"

    files {
        "sandbox/src/**.c",
        "sandbox/src/**.h"
    }

    filter "configurations:Debug"
        symbols "On"

    filter "configurations:Release or configurations:Dist"
        optimize "On"

使用gmake2生成器生成Makefile后,执行make时出现错误:

==== Building runtime (debug) ====
Creating ../bin/Debug
mkdir: cannot create directory '..\\bin\\Debug': No such file or directory
make[1]: *** [Makefile:88: ../bin/Debug] Error 1
make: *** [Makefile:36: runtime] Error 2

原因是mkdir尝试创建../bin/Debug时,上层的bin目录尚未存在,导致创建失败。

解决办法

方法1:手动提前创建父目录

在项目根目录执行命令,提前创建bin和obj目录:

  • 若使用Git Bash/WSL终端:
mkdir bin obj
  • 若使用Windows CMD:
md bin obj

方法2:修改Premake脚本,构建时自动创建父目录

在根目录的premake5.lua中添加预构建命令,确保在编译前创建必要的父目录:

workspace "epoch"
    configurations { "Debug", "Release", "Dist" }
    architecture "x86_64"
    startproject "sandbox"

    -- 预构建阶段检查并创建bin、obj目录
    prebuildcommands {
        "if not exist bin mkdir bin",
        "if not exist obj mkdir obj"
    }

include "runtime/runtime.lua"
include "sandbox/sandbox.lua"

该命令会在每个项目构建前执行,确保父目录存在,无需手动操作。

方法3:修改Premake脚本,生成Makefile时自动创建目录

在根目录premake5.lua开头添加Premake内置的目录创建函数,在生成Makefile的阶段就创建好bin和obj:

-- 生成Makefile时提前创建根目录下的bin、obj文件夹
os.mkdir("bin")
os.mkdir("obj")

workspace "epoch"
    configurations { "Debug", "Release", "Dist" }
    architecture "x86_64"
    startproject "sandbox"

include "runtime/runtime.lua"
include "sandbox/sandbox.lua"

这种方式在执行premake5 gmake2时就会创建目录,避免构建阶段的mkdir错误。


内容的提问来源于stack exchange,提问作者guyus15

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 03:45:01