Cmake 编译器问题

Sup*_*etz 2 c++ mingw makefile cmake

我正在尝试cmake使用 Windows 10 进行设置MinGW。我已将路径包含c:/MinGW/bin在系统路径和环境路径设置中。我已经sh.exe从我的道路上移除了(虽然,如果可能的话,我希望能够保留它)。

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/gcc.exe")

project (Tutorial)
add_executable(Tutorial tutorial.cpp)
Run Code Online (Sandbox Code Playgroud)

输出

C:\School\athabascua\data structures\ass1>cmake -g "MinGW Makefiles" .
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: "nmake" "/NOLOGO" "cmTC_b3144\fast"
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler "C:/MinGW/bin/gcc.exe" is not able to compile a simple test
program.

It fails with the following output:

Change Dir: C:/School/athabascua/data structures/ass1/CMakeFiles/CMakeTmp



Run Build Command:"nmake" "/NOLOGO" "cmTC_b3144\fast"



Generator: execution of make failed.  Make command was: "nmake" "/NOLOGO"
"cmTC_b3144\fast"
Run Code Online (Sandbox Code Playgroud)

似乎 GNU 编译器已被识别,但似乎不起作用。任何帮助将非常感激。我试图避免使用 Cygwin ......但几乎准备好在这里一秒钟内走这条路。

Sup*_*etz 5

为了解决我遇到的问题,我必须做两件事。

  1. 使用命令cmake -G "MinGW Makefiles" .(windows 上的大写 -G)
  2. 更新我的CMakeLists.txt文件,将 gcc 用于 C 编译器,将 g++ 用于 C++ 编译器

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe")

project (Tutorial)
add_executable(Tutorial tutorial.cpp)
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,您可以避免在 CMakeLists.txt 中设置变量,从而通过在命令行中指定它们来保持程序可移植性`-DCMAKE_C_COMPILER="C:/MinGW/bin/gcc.exe" -DCMAKE_CXX_COMPILER="C: /MinGW/bin/g++.exe"`。 (3认同)
  • 是的,`gcc` 和 `g++` 都运行相同的编译器,但具有不同的默认选项。对于链接步骤,文件都命名为“*.o”,因此文件名没有提示应该使用 C++ 库。 (2认同)