Windows下Premake5生成错误文件分隔符?求助排查配置问题
Windows下Premake5构建C项目失败排查
错误信息
执行构建命令时出现如下错误:
$ make config=release_win64 "==== Building calc (release_win64) ====" Creating obj/win64/Release "main.c" process_begin: CreateProcess(NULL, cc -MD -MP -DNDEBUG -Isrc/include -m64 -Werror -flto -O3 -o obj/win64/Release/main.o -MF obj/win64/Release/main.d -c src/main.c, ...) failed. make (e=2): The system cannot find the file specified. make[1]: *** [calc.make:153: obj/win64/Release/main.o] Error 2 make: *** [Makefile:36: calc] Error 2
项目目录结构
- Project Root - src - include - sum.h - main.c - sum.c - premake5.lua
premake5.lua脚本
workspace "calc" configurations { "Debug", "Release" } platforms { "win64", "linux64" } project "calc" kind "ConsoleApp" language "C" targetname "calc" targetdir "bin/%{cfg.buildcfg}" entrypoint ("main()") includedirs { "src/include" } files { "src/**.h", "src/**.c" } filter { "platforms:linux64" } system "linux" architecture "x86_64" filter { "platforms:win64" } system "windows" architecture "x86_64" toolset "gcc" filter { "configurations:Debug" } flags { "FatalWarnings" } defines { "DEBUG" } symbols "On" filter { "configurations:Release" } flags { "FatalWarnings", "LinkTimeOptimization" } defines { "NDEBUG" } optimize "speed"
源码文件
main.c
#include "include/sum.h" #include <stdio.h> int main() { int a = 1; int b = 2; int result = sum(a, b); printf("%d + %d = %d", a, b, result); return 0; }
sum.h
#ifndef SUM_H #define SUM_H int sum(int a, int b); #endif
sum.c
#include "include/sum.h" int sum(int a, int b) { return a + b; }
构建命令
$ premake5 gmake2 Building configurations... Running action 'gmake2'... Done (57ms). $ make config=release_win64 "==== Building calc (release_win64) ====" "main.c" process_begin: CreateProcess(NULL, cc -MD -MP -DNDEBUG -Isrc/include -m64 -Werror -flto -O3 -o obj/win64/Release/main.o -MF obj/win64/Release/main.d -c src/main.c, ...) failed. make (e=2): The system cannot find the file specified. make[1]: *** [calc.make:153: obj/win64/Release/main.o] Error 2 make: *** [Makefile:36: calc] Error 2
自动生成的Makefile(摘要)
# Alternative GNU Make workspace makefile autogenerated by Premake ifndef config config=debug_win64 endif ifndef verbose SILENT = @ endif # ... 省略中间内容 ... calc: ifneq (,$(calc_config)) @echo "==== Building calc ($(calc_config)) ====" @${MAKE} --no-print-directory -C . -f calc.make config=$(calc_config) endif
自动生成的calc.make(关键部分)
# ... 省略开头内容 ... LINKCMD = $(CC) -o "$@" $(OBJECTS) $(RESOURCES) $(ALL_LDFLAGS) $(LIBS) # ... 省略配置内容 ... $(OBJDIR)/main.o: src/main.c @echo "$(notdir $<)" $(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<" $(OBJDIR)/sum.o: src/sum.c @echo "$(notdir $<)" $(SILENT) $(CC) $(ALL_CFLAGS) $(FORCE_INCLUDE) -o "$@" -MF "$(@:%.o=%.d)" -c "$<"
环境信息
- Make版本:
$ make --version GNU Make 4.4.1 Built for Windows32 Copyright (C) 1988-2023 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
- GCC版本:
$ gcc --version gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0 Copyright (C) 2018 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- 操作系统:Windows 11 23H2(OS build 22631.4169)
- PATH环境变量:包含
C:\mingw64\bin和C:\Program Files\premake-5.0.0-beta2-windows
问题分析
错误核心是系统找不到cc命令。Windows下MinGW提供的C编译器是gcc.exe,而非类Unix系统通用的cc。生成的calc.make中使用了$(CC)变量,但该变量在当前环境下未被定义为gcc,导致Make尝试调用不存在的cc命令。
另外,源码中头文件引用存在冗余:main.c和sum.c里写#include "include/sum.h",但项目已通过includedirs指定src/include为头文件目录,直接写#include "sum.h"即可,避免路径解析错误。
解决方案
方案1:修改premake5.lua配置
将win64平台的toolset指定为mingw,让Premake生成适配MinGW的Makefile:
filter { "platforms:win64" } system "windows" architecture "x86_64" toolset "mingw" -- 替换原有的toolset "gcc"
修改后重新生成Makefile并构建:
premake5 gmake2 make config=release_win64
方案2:构建时显式指定编译器变量
无需修改脚本,执行make命令时直接指定CC=gcc:
make config=release_win64 CC=gcc
补充:修复头文件引用
将main.c和sum.c中的#include "include/sum.h"改为#include "sum.h",匹配includedirs的配置。
内容的提问来源于stack exchange,提问作者codemaniac
相关产品推荐
相关产品推荐

