SCons基础示例报错:无法生成目标文件`helloworld`
SCons构建HelloWorld报错排查:"Do not know how to make File target"
场景与代码
按照SCons教程构建简单C程序,已创建如下文件:
SConstruct 文件
env = Environment() # construction environment env.Program(target='helloworld', source=['helloworld.c']) # build targets
helloworld.c 文件
#include <stdio.h> int main() { printf("Hello World."); }
在PowerShell中执行命令 scons helloworld 时,出现报错:
scons: Reading SConscript files ... scons: done reading SConscript files. scons: Building targets ... scons: building terminated because of errors. scons: *** Do not know how to make File target `helloworld` (..\scons-tutorial\helloworld). Stop.
问题原因
Windows环境下,SCons构建C程序会默认给可执行文件添加.exe后缀。你在SConstruct中指定的target是helloworld,但实际生成的可执行文件会是helloworld.exe;当你执行scons helloworld时,SCons找不到无后缀的helloworld目标文件,因此抛出该错误。
解决方法
你可以任选一种方式修复:
- 方式1:执行命令时指定完整目标名
在PowerShell中运行:scons helloworld.exe - 方式2:修改SConstruct明确指定带后缀的目标
更新SConstruct代码:env = Environment() env.Program(target='helloworld.exe', source=['helloworld.c']) - 方式3:不指定target,让SCons自动生成
简化SConstruct,SCons会自动根据源文件名生成带.exe后缀的可执行文件:
此时直接执行env = Environment() env.Program(source=['helloworld.c'])scons即可完成构建。
额外检查项
- 确保SConstruct和helloworld.c在同一目录,且执行
scons命令时的工作目录就是该目录; - 确认系统已安装C编译器(如MinGW、MSVC),且编译器已配置到系统PATH中,保证SCons能正常调用编译器。
内容的提问来源于stack exchange,提问作者user32882
相关产品推荐
相关产品推荐

