cmake:指定编译器的问题

Pie*_*tro 3 windows gcc makefile g++ cmake

我有这个CMakeLists.txt文件的问题:

cmake_minimum_required(VERSION 2.6)

SET(CMAKE_C_COMPILER C:\\MinGW\\bin\\gcc)
SET(CMAKE_CXX_COMPILER C:\\MinGW\\bin\\g++)

project(cmake_test)

add_executable(a.exe test.cpp)
Run Code Online (Sandbox Code Playgroud)

用以下方式调用cmake: cmake -G "MinGW Makefiles"

我明白了:

c:\cmake_test>cmake -G "MinGW Makefiles" .
-- The C compiler identification is GNU 4.6.1
CMake Warning (dev) at CMakeFiles/CMakeCCompiler.cmake:1 (SET):
  Syntax error in cmake code at

    C:/cmake_test/CMakeFiles/CMakeCCompiler.cmake:1

  when parsing string

    C:\MinGW\bin\gcc

  Invalid escape sequence \M

  Policy CMP0010 is not set: Bad variable reference syntax is an error.  Run
  "cmake --help-policy CMP0010" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.
Call Stack (most recent call first):
  CMakeLists.txt:12 (project)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The CXX compiler identification is GNU 4.6.1
CMake Warning (dev) at CMakeFiles/CMakeCXXCompiler.cmake:1 (SET):
  Syntax error in cmake code at

    C:/cmake_test/CMakeFiles/CMakeCXXCompiler.cmake:1

  when parsing string

    C:\MinGW\bin\g++

  Invalid escape sequence \M

  Policy CMP0010 is not set: Bad variable reference syntax is an error.  Run
  "cmake --help-policy CMP0010" for policy details.  Use the cmake_policy
  command to set the policy and suppress this warning.
Call Stack (most recent call first):
  CMakeLists.txt:12 (project)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Check for working C compiler: C:\MinGW\bin\gcc
CMake Error at C:/cmake_test/CMakeFiles/CMakeCCompiler.cmake:1 (SET):
  Syntax error in cmake code at

    C:/cmake_test/CMakeFiles/CMakeCCompiler.cmake:1

  when parsing string

    C:\MinGW\bin\gcc

  Invalid escape sequence \M
Call Stack (most recent call first):
  CMakeLists.txt:2 (PROJECT)

CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: Internal CMake error, TryCompile configure of cmake failed
CMake Error: your C compiler: "C:\MinGW\bin\gcc" was not found.   Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "C:\MinGW\bin\g++" was not found.   Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

C:\MinGW\bin\g++
/C/MinGW/bin/g++
Run Code Online (Sandbox Code Playgroud)

结果相同.双引号也没有帮助.

当然,编译器存在于指定的目录中.

谢谢你的提示.

nne*_*neo 5

你必须使用四个反斜杠来获得一个文字反斜杠SET:

SET(CMAKE_C_COMPILER C:\\\\MinGW\\\\bin\\\\gcc)
SET(CMAKE_CXX_COMPILER C:\\\\MinGW\\\\bin\\\\g++)
Run Code Online (Sandbox Code Playgroud)

无论您是否在参数周围使用引号,这都适用.

由于这非常难看,使用正斜杠可能更好:

SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc)
SET(CMAKE_CXX_COMPILER C:/MinGW/bin/g++)
Run Code Online (Sandbox Code Playgroud)