使用 Mingw-w64 编译谷歌测试
Posted
技术标签:
【中文标题】使用 Mingw-w64 编译谷歌测试【英文标题】:Compiling Google test with Mingw-w64 【发布时间】:2019-11-08 00:01:49 【问题描述】:我正在尝试在 Windows 10 机器上使用 Mingw-w64 编译 Google 测试,但我总是收到错误:
C:\git\tdd\googletest>cmake CMakeLists.txt -G "MinGW Makefiles"
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles". CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
这就是我正在做的:
-
打开一个 Windows CMD 终端并更改我下载 google test 的文件夹 t
将 CMake、make 和 g++ 添加到路径
set PATH="C:\Program Files\CMake\bin";"C:\msys\bin";"C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin";%PATH%
使用cmake CMakeLists.txt -G "MinGW Makefiles"
运行 CMake
这些是工具的版本:
GNU Make 3.81
Copyright (C) 2006 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.
This program built for i686-pc-msys
cmake 版本 3.15.3
g++ (i686-posix-dwarf-rev0, 由 MinGW-W64 项目构建) 8.1.0
Make 来自MSYS package from Mingw-64w。我从 here 克隆了 Google 测试
非常感谢任何帮助!
编辑 1
CMake/MinGW unknown compilers, gcc.exe broken 中的解决方案均无效。
编辑 2
我还尝试使用环境变量来告诉 CMake 使用哪些工具。
-
将工具文件夹添加到 PATH:
set PATH="C:\Program Files\CMake\bin";"C:\Program Files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\bin";%PATH%
-
使用 CMake 调用
cmake -DCMAKE_C_COMPILER="C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gcc" -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/g++" -DCMAKE_MAKE_PROGRAM="C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/mingw32-make.exe" CMakeLists.txt -G "MinGW Makefiles"
我收到此错误:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:10 (project):
The CMAKE_C_COMPILER:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/gcc
is not a full path to an existing compiler tool.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
CMake Error at CMakeLists.txt:10 (project):
The CMAKE_CXX_COMPILER:
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/g++
is not a full path to an existing compiler tool.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
See also "C:/git/tdd/googletest/CMakeFiles/CMakeOutput.log".
See also "C:/git/tdd/googletest/CMakeFiles/CMakeError.log".
由于 g++ 和 gcc 在 PATH 中,我也尝试像这样调用 CMake:
cmake -DCMAKE_C_COMPILER="gcc" -DCMAKE_CXX_COMPILER="g++" -DCMAKE_MAKE_PROGRAM="mingw32-make.exe" CMakeLists.txt -G "MinGW Makefiles"
这会产生与上一次尝试相同的错误:
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:10 (project):
The CMAKE_C_COMPILER:
gcc
is not a full path and was not found in the PATH.
Tell CMake where to find the compiler by setting either the environment
variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
CMake Error at CMakeLists.txt:10 (project):
The CMAKE_CXX_COMPILER:
g++
is not a full path and was not found in the PATH.
Tell CMake where to find the compiler by setting either the environment
variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
-- Configuring incomplete, errors occurred!
See also "C:/git/tdd/googletest/CMakeFiles/CMakeOutput.log".
See also "C:/git/tdd/googletest/CMakeFiles/CMakeError.log".
但是,我可以致电g++
:
C:\git\tdd\googletest>g++
g++: fatal error: no input files
compilation terminated.
再次感谢!
【问题讨论】:
【参考方案1】:您最初的问题源于选择为 MSYS2 使用 make.exe 连同 MinGW-w64 编译器,该版本的 make.exe 不支持该编译器。与 MinGW-w64 一起使用的正确程序是 mingw32-make.exe,它是 MinGW-w64 发行版的一部分。我不能完全理解您以后的编辑,但例如这在 cmd.exe 中运行良好:
set "PATH=C:\Program Files\CMake\bin;%PATH%"
set "PATH=C:\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin;%PATH%"
cmake ^
-G "MinGW Makefiles" ^
-D CMAKE_C_COMPILER=gcc.exe ^
-D CMAKE_CXX_COMPILER=g++.exe ^
-D CMAKE_MAKE_PROGRAM=mingw32-make.exe ^
.
mingw32-make.exe
这里,我用过:
CMake 3.15.2, MinGW-w64 8.1.0 (x86_64-8.1.0-posix-seh-rt_v6-rev0), 以及 Google Test 的当前 git 版本。【讨论】:
【参考方案2】:好的,我设法使用 CMDER 的 Bash 控制台对其进行了编译。假设您在 C:\mingw-w64\
安装了 Mingw-w64(路径中没有空格),这些步骤将为您提供一个在 Mingw-w64 下工作的 Google 测试库
-
将 PATH 设置为:
PATH=/c/Program\ Files/CMake/bin:/c/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin:$PATH
-
使用 CMake 调用
cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_MAKE_PROGRAM=mingw32-make.exe CMakeLists.txt -G 'MSYS Makefiles'
-
使用构建
mingw32-make.exe -j8
我会在这里勉强说这可能应该与 Git Bash shell 一起使用。我仍然没有使用常规 Windows CMD 终端构建它的解决方案。
【讨论】:
以上是关于使用 Mingw-w64 编译谷歌测试的主要内容,如果未能解决你的问题,请参考以下文章