49

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
jww
  • 97,681
  • 90
  • 411
  • 885
Pietro
  • 12,086
  • 26
  • 100
  • 193
  • 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? – Benjamin Bannier Oct 24 '12 at 17:26
  • [I thought the same](http://stackoverflow.com/questions/13050827/cmake-problems-specifying-the-compiler#comment17722760_13050827) – Pietro Oct 24 '12 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. – DLRdave Oct 26 '12 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? – DLRdave Oct 26 '12 at 15:24

4 Answers4

82

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.

MCCCS
  • 1,002
  • 3
  • 20
  • 44
DLRdave
  • 13,876
  • 4
  • 53
  • 70
  • 3
    The original problem which the author posted is not answered. Method #3 might be needed at times. Say you are cross compiling. – leodotcloud Apr 29 '15 at 01:18
  • 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. – DLRdave Apr 29 '15 at 15:50
  • If cross compiling you can set `CMAKE_TOOLCHAIN_FILE` on the command line. [Wiki link.](http://www.cmake.org/Wiki/CMake_Cross_Compiling#The_toolchain_file) – midrare Aug 29 '15 at 22:27
  • 1
    @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. – Royi Feb 20 '18 at 01:44
  • 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? – DLRdave Feb 20 '18 at 18:11
  • @DLRdave, It seemed the only way was `cmake .. -G"Visual Studio 15 2017 Win64" -T"Intel C++ Compiler 18.0"`. If you're aware of any other way to use different compiler with CMake I'd be happy to hear. – Royi Apr 19 '18 at 12:16
  • It depends on the -G arg you use. With Visual Studio generators, -T may be one way to use a different compiler. With makefile based generators, and the ninja generator, the CC and CXX env variables are the right way to approach this. – DLRdave Apr 19 '18 at 17:10
  • Unfortunately doesn't work for me to select gcc installed from brew on OSX 12.1 – nmr Feb 24 '22 at 00:19
  • Well, I can't edit my comment and I can't undo my downvote, which is frustrating because I was wrong. It works fine, it just happens that /usr/bin/gcc is actually a copy of clang which is some user-hateful garbage I hadn't anticipated. – nmr Feb 24 '22 at 15:37
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

junghyun jun
  • 81
  • 1
  • 1
  • 1
    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 M Aug 08 '17 at 23:07
  • 5
    @Ben. Of course it is helpful. More even if it is that specific case!! – Brethlosze Oct 04 '17 at 15:02
  • @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 – Ben M Oct 09 '17 at 17:58
  • 7
    I dont know in which world you live, but adding new information to an existing answer is not "noise". – Brethlosze Oct 09 '17 at 20:06
  • 1
    This answer actually saved my @$$. Bravo Junghyun! – AmirSojoodi May 16 '20 at 17:59
4

Using with FILEPATH option might work:

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

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++.

Joel
  • 167
  • 1
  • 1
  • 11