【问题标题】:cmake: problems specifying the compilercmake:指定编译器的问题
【发布时间】:2012-10-14 14:07:06
【问题描述】:

我对这个 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)

使用:cmake -G "MinGW Makefiles" 调用 cmake

我明白了:

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!

我也试过了:

C:\MinGW\bin\g++
/C/MinGW/bin/g++

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

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

感谢您的任何提示。

【问题讨论】:

  • SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc) 更容易使用。
  • 不错,Windows 和 Unix 语法的混合体 ;-)
  • 仅供参考:自 DOS 2.0! 起支持正斜杠
  • 看起来你需要在文件名的末尾加上“.exe”

标签: windows gcc makefile g++ cmake


【解决方案1】:

您必须使用 四个 反斜杠来获得 SET 中的文字反斜杠:

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

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

由于这很丑陋,最好使用正斜杠:

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

【讨论】:

  • 这是我第一次看到四个反斜杠在一起!他们应该将其添加到文档中...
  • 当设置一个 CMake 变量是一个目录或文件名的路径时,使用“/”作为分隔符。
  • 但是,您应该避免直接在 CMakeLists.txt 文件中设置 _COMPILER 变量。请参阅我对此问题的“(2)”版本的回答:stackoverflow.com/a/13089688/236192
猜你喜欢
  • 1970-01-01
  • 2017-01-19
  • 2012-10-13
  • 1970-01-01
  • 2021-04-22
  • 2012-10-14
相关资源
最近更新 更多