【问题标题】:SCons cannot find the mingw-w64 compilerSCons 找不到 mingw-w64 编译器
【发布时间】:2014-09-07 13:43:23
【问题描述】:

我最近安装了 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 是它的第二个偏好。我猜这与上述问题有关。

【问题讨论】:

    标签: scons mingw-w64


    【解决方案1】:

    SCons FAQ(可在项目网站https://www.scons.org 上找到)回答了这个最常见的问题,Why doesn't SCons find my compiler/linker

    您必须将您的 shell PATH 传播到您的构建环境。默认情况下,SCons 创建干净的构建环境,其中没有任何设置,这样可以保证可重复构建。

    如常见问题解答中所述,将以下内容添加到您的 SConstruct:

    import os
    env = Environment(tools = ['mingw'], ENV = {'PATH' : os.environ['PATH']})
    

    【讨论】:

      【解决方案2】:

      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 的符号链接来解决 mingw-w64 的此问题.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.
      

      仅供参考,这是 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)..
      • 好的,已将电子邮件发送到 scons 开发人员列表。希望这个问题很快得到解决。
      • 实际上它并没有坏掉(据说)。它按照手册页中的说明执行(见下文)。当前执行此操作的方法是将 mingw-w64 的 bin 目录的路径添加到 env['ENV']['PATH']。我只是建议对 SCons 的增强是检查更多默认路径。
      【解决方案3】:

      scons 手册页也有一个关于使用 MINGW 的 sn-p:

      MinGW

      MinGW bin 目录必须在您的 PATH 环境变量中,或者 SCons 的 ENV 构造变量下的 PATH 变量 检测并使用 MinGW 工具。在原生 Windows 下运行时 Python 解释器,SCons 将更喜欢 MinGW 工具而不是 Cygwin 工具,如果它们都已安装,则无论 bin 的顺序如何 PATH 变量中的目录。如果您同时拥有 MSVC 和 MinGW 已安装并且您想使用 MinGW 而不是 MSVC,那么您必须 通过传递明确告诉 SCons 使用 MinGW

      tools=['mingw'] 到 Environment() 函数,因为 SCons 将 比起 MinGW 工具更喜欢 MSVC 工具

      也就是说,向 SCons 的 MINGW 的默认搜索路径添加更多默认位置可能是有意义的。

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

      同时搜索“mingw32-gcc”

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-27
        • 1970-01-01
        • 1970-01-01
        • 2019-10-27
        • 1970-01-01
        • 1970-01-01
        • 2020-07-27
        • 1970-01-01
        相关资源
        最近更新 更多