50

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" , it fails with the following output:

c:\Users\pietro.mele\projects\tests\buildSystem_test\cmake_test>cmake -G "MinGW Makefiles" .
-- The C compiler identification is GNU 4.6.1
-- The CXX compiler identification is GNU 4.6.1
-- Check for working C compiler: C:/MinGW/bin/gcc
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: Internal CMake error, TryCompile configure of cmake failed
-- Check for working C compiler: C:/MinGW/bin/gcc -- broken
CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/CMakeTestCCompiler.cmake:52 (MESSAGE):
  The C compiler "C:/MinGW/bin/gcc" is not able to compile a simple test
  program.

CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:10 (project)

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!

However the gcc compiler is in C:/MinGW/bin/ and it works.

Any idea?

Platform:

  • Windows 7
  • MinGW/GCC 4.6
4
  • I haven't touched Windows in a long time, but does it bother cmake that the OS usually says C:\MinGW\bin\g++ instead of what you specified? Commented Oct 24, 2012 at 17:26
  • I thought the same Commented Oct 24, 2012 at 17:30
  • 1
    No, it doesn't bother CMake. CMake uses "/" as separator characters regardless of OS. On Windows, we translate to "\" whenever necessary. Commented Oct 26, 2012 at 15:23
  • 3
    By the way, since the error message here is that it's "not found" -- perhaps you need to add the ".exe" to the end of the file names? Commented Oct 26, 2012 at 15:24

4 Answers 4

85

Never try to set the compiler in the CMakeLists.txt file.

See the CMake FAQ about how to use a different compiler:

https://gitlab.kitware.com/cmake/community/wikis/FAQ#how-do-i-use-a-different-compiler

(Note that you are attempting method #3 and the FAQ says "(avoid)"...)

We recommend avoiding the "in the CMakeLists" technique because there are problems with it when a different compiler was used for a first configure, and then the CMakeLists file changes to try setting a different compiler... And because the intent of a CMakeLists file should be to work with multiple compilers, according to the preference of the developer running CMake.

The best method is to set the environment variables CC and CXX before calling CMake for the very first time in a build tree.

After CMake detects what compilers to use, it saves them in the CMakeCache.txt file so that it can still generate proper build systems even if those variables disappear from the environment...

If you ever need to change compilers, you need to start with a fresh build tree.

Sign up to request clarification or add additional context in comments.

9 Comments

The original problem which the author posted is not answered. Method #3 might be needed at times. Say you are cross compiling.
I think the original problem is answered. If the poster tries to use the environment variable method, rather than setting the variables in the CMakeLists.txt file, I think it will work, and he will not get the error.
If cross compiling you can set CMAKE_TOOLCHAIN_FILE on the command line. Wiki link.
@DLRdave, Is something different on CMake 3.10? As non of these work for me. It always use MSVC even if I set the Environment Variables or use the OP method. Thank You.
I am unaware of changes in CMake 3.10 that would change anything discussed here. Are you calling CMake from the same environment where you set the environment variables? Or are you setting them and the launching the CMake GUI separately?
|
7

I had similar problem as Pietro,

I am on Window 10 and using "Git Bash". I tried to execute >>cmake -G "MinGW Makefiles", but I got the same error as Pietro.

Then, I tried >>cmake -G "MSYS Makefiles", but realized that I need to set my environment correctly.

Make sure set a path to C:\MinGW\msys\1.0\bin and check if you have gcc.exe there. If gcc.exe is not there then you have to run C:/MinGW/bin/mingw-get.exe and install gcc from MSYS.

After that it works fine for me

5 Comments

Hey Junghyun, just as an FYI - it's generally not very helpful to add new answers to already answered questions, especially if your answer is a more specific case of the given general answer. Cheers!
@Ben. Of course it is helpful. More even if it is that specific case!!
@hyprfrcb imagine a world where everyone who had a similar error message just added a new answer with whatever solved their version of the error message on their machine. They wouldn't be answering the original question, they'd just be providing a solution to a related but different problem. (In this case, the new answer had the same ERROR but it was not the same PROBLEM. New answerer did not have GCC installed whereas original user was setting the path incorrectly). If everyone did this it would result in a lot of not helpful noise on every question
I dont know in which world you live, but adding new information to an existing answer is not "noise".
This answer actually saved my @$$. Bravo Junghyun!
3

Using with FILEPATH option might work:

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

Comments

1

I had the same issue. And in my case the fix was pretty simple. The trick is to simply add the ".exe" to your compilers path. So, instead of :

SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc)

It should be

SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc.exe)

The same applies for g++.

Comments

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.