Windows 下scons执行编译错误 'cl' 不是内部或外部命令,也不是可运行的程序 解决方法

系统是Win10,最近研究scons编译脚本,pip下载了scons。然后写了一个简单的c文件

/* file: hello.c */
#include <stdio.h>
int main(int argc, char** argv)
{
	printf("Hello, world!\n");
	return 0;
}

创建SConstruct文件

Program('program', Glob('*.c'))

然后在该文件夹启动命令提示符,执行scons命令,返回报错:

E:\test_scons>scons
scons: Reading SConscript files ...

scons: warning: No version of Visual Studio compiler found - C/C++ compilers most likely not set correctly
File "E:\test_scons\SConstruct", line 2, in <module>
scons: done reading SConscript files.
scons: Building targets ...
cl /Fohello.obj /c hello.c /nologo
'cl' 不是内部或外部命令,也不是可运行的程序

或批处理文件。
scons: *** [hello.obj] Error 1
scons: building terminated because of errors.

从报错信息可以看出找不到cl程序。

新建myscons.bat,内容如下

cmd /k "%VS80COMNTOOLS%vsvars32.bat"

保存双击运行,然后执行scons,依然同样一个错误。怀疑scons调用编译命令有问题,对scons命令流程进行了一系列打印跟踪,最终发现scons执行编译命令会调用

C:\Python27\Lib\site-packages\scons\SCons\Platform\win32.py 下spawn这个函数

def spawn(sh, escape, cmd, args, env):
    if not sh:
        sys.stderr.write("scons: Could not find command interpreter, is it in your PATH?\n")
        return 127
    return exec_spawn([sh, '/C', escape(' '.join(args))], env)

exec_spawn参数1里面的sh是cmd, 相当于执行了cmd /c ***,这样有问题,前面设置的vsvars32.bat就会没作用了,修改return那行代码为

return os.system(escape(' '.join(args)))

即可正确执行sons命令啦,打印如下:

E:\test_scons>scons
scons: Reading SConscript files ...

scons: warning: No version of Visual Studio compiler found - C/C++ compilers most likely not set correctly
File "E:\test_scons\SConstruct", line 1, in <module>
scons: done reading SConscript files.
scons: Building targets ...
cl /Fohello.obj /c hello.c /nologo
hello.c
link /nologo /OUT:program.exe hello.obj
scons: done building targets.

正确生成了program.exe。

猜你喜欢

转载自blog.csdn.net/haart/article/details/88534141