SCons无法找到mingw-w64编译器。

3

我最近安装了mingw-w64,并尝试使用SCons构建程序。这是我一直在尝试使用的SConstruct文件示例:

env = Environment(tools = ['mingw'])
env.Program('test.c')

然而,我遇到了以下错误:
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...

scons: warning: No version of Visual Studio compiler found - C/C++ compilers most likely not set correctly
File "C:\Python27\Scripts\scons.py", line 199, in <module>
gcc -o test.o -c test.c
'gcc' is not recognized as an internal or external command,
operable program or batch file.
scons: *** [test.o] Error 1
scons: building terminated because of errors.

我已经正确设置了PATH变量,并可以通过命令行使用gcc,所以很奇怪SCons找不到它。gcc的位置是C:\Program Files\mingw-w64\mingw64\bin

另一件我觉得奇怪的事情是需要指定工具。手册上说mingw是第二选择。我猜这与上述问题有关。

3个回答

6
The SCons FAQ (可以在项目网站https://www.scons.org中找到)回答了这个最常问的问题,为什么SCons无法找到我的编译器/链接器等?我在命令行中可以正常执行。 您需要将您的shell PATH传播到您的构建环境中。默认情况下,SCons创建干净的构建环境,没有任何设置,以便可以保证重复构建。
如FAQ所述,请将以下内容添加到您的SConstruct文件中:
import os
env = Environment(tools = ['mingw'], ENV = {'PATH' : os.environ['PATH']})

3
Scons无法正确检测mingw-w64。
默认情况下,Scons将搜索多个编译器直到找到一个。但是对于mingw,它会寻找一个名为mingw32-gcc的文件,这对于mingw-w64不起作用。
从源代码可以看出:https://bitbucket.org/scons/scons/raw/default/src/engine/SCons/Tool/mingw.py
# This is what we search for to find mingw:
key_program = 'mingw32-gcc'

您可以通过在mingw64 \ bin目录中创建一个符号链接来解决这个问题,将mingw32-gcc.exe指向gcc.exe(以管理员身份运行cmd.exe或mklink无法工作):
 c:\...\mingw64\bin> mklink mingw32-gcc.exe gcc.exe

您可以通过使用“dir”命令来验证符号链接:
10/27/2017  01:06 PM    <SYMLINK>      mingw32-gcc.exe [gcc.exe]

有了这个,一个简单的SConstruct就可以工作了,假设您的路径中有mingw-w64 gcc.exe。
d:\test>type SConstruct
Program('hello.c')

d:\test>scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
gcc -o hello.o -c hello.c
gcc -o hello.exe hello.o
scons: done building targets.

FYI,这是Scons在Windows上搜索的工具列表: https://bitbucket.org/scons/scons/raw/default/src/engine/SCons/Tool/__init__.py
def tool_list(platform, env):
    ...
    if str(platform) == 'win32':
        "prefer Microsoft tools on Windows"
        linkers = ['mslink', 'gnulink', 'ilink', 'linkloc', 'ilink32' ]
        c_compilers = ['msvc', 'mingw', 'gcc', 'intelc', 'icl', 'icc', 'cc', 'bcc32' ]
        cxx_compilers = ['msvc', 'intelc', 'icc', 'g++', 'cxx', 'bcc32' ]
        ...

你能给scons用户邮件列表发送一封电子邮件吗?听起来像是一个不太难解决的错误。(而且不需要做mklink) - undefined
好的,已发送电子邮件给scons开发者列表。希望这个问题很快得到解决。 - undefined
实际上它并没有损坏(本身)。它表现出manpage中指示的方式(见下文)。目前执行此操作的方法是将路径添加到mingw-w64的bin目录,然后添加到env ['ENV'] ['PATH']。 我只是建议增强SCons的方法是检查更多默认路径。 - undefined

1

此外,scons的手册中有一段关于使用MINGW的片段:

MinGW

必须将MinGW bin目录添加到您的PATH环境变量或ENV构造变量下的PATH变量中,以便SCons检测和使用MinGW工具。当在本地Windows Python解释器下运行时,如果同时安装了Cygwin工具和MinGW工具,则SCons会优先选择MinGW工具,而不管PATH变量中bin目录的顺序。如果您同时安装了MSVC和MinGW,并且想要使用MinGW而不是MSVC,则必须通过向Environment()函数传递tools=['mingw']来显式告诉SCons使用MinGW,因为SCons会优先选择MSVC工具而不是MinGW工具。

话虽如此,也许将更多默认位置添加到SCons的默认搜索路径中以搜索MINGW是有意义的。

目前它将检查Environment()的PATH,然后查看c:\MinGW\bin

在两个位置都搜索"mingw32-gcc"

但对于mingw-w64, 默认的二进制文件似乎是gcc.exe和/或x86_64-w64-mingw32-gcc.exe(或i686-w64-mingw32-gcc.exe)?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接