6

I have a problem with this CMakeLists.txt file:

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)

Calling cmake with: cmake -G "MinGW Makefiles"

I get:

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!

I also tried with:

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

with the same result. Double quotes have not been helpful, either.

Of course, the compiler is present in the specified directory.

Thank you for any hint.

4
  • 2
    SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc) is a bit easier to use. Commented Oct 24, 2012 at 14:29
  • Nice, a mixture of Windows and Unix syntax ;-) Commented Oct 24, 2012 at 17:04
  • FYI: forward slashes have been supported since DOS 2.0! Commented Oct 24, 2012 at 17:42
  • and it looks like you need ".exe" on the end of the filenames Commented Oct 26, 2012 at 15:28

1 Answer 1

6

You have to use four backslashes to get a literal backslash in a SET:

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

This applies whether or not you use quotes around the argument.

Since this is pretty ugly, it's probably better to use forward slashes:

SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc)
SET(CMAKE_CXX_COMPILER C:/MinGW/bin/g++)
Sign up to request clarification or add additional context in comments.

3 Comments

It is the first time I see four backslashes together! They should add it to the documentation...
When setting a CMake variable that is a path to a directory or file name, use "/" as the separator character.
However, you should avoid setting the _COMPILER variables directly in the CMakeLists.txt file. See my answer to your "(2)" version of this question: stackoverflow.com/a/13089688/236192

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.