2

I am trying to setup cmake using Windows 10 using MinGW. I have included the path c:/MinGW/bin in my system path and environment path settings. I have removed sh.exe from my path (although, i would love to be able to keep this if possible).

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

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

project (Tutorial)
add_executable(Tutorial tutorial.cpp)

Output

C:\School\athabascua\data structures\ass1>cmake -g "MinGW Makefiles" .
-- The C compiler identification is GNU 5.3.0
-- The CXX compiler identification is GNU 5.3.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: "nmake" "/NOLOGO" "cmTC_b3144\fast"
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.8/Modules/CMakeTestCCompiler.cmake:51 (message):
The C compiler "C:/MinGW/bin/gcc.exe" is not able to compile a simple test
program.

It fails with the following output:

Change Dir: C:/School/athabascua/data structures/ass1/CMakeFiles/CMakeTmp



Run Build Command:"nmake" "/NOLOGO" "cmTC_b3144\fast"



Generator: execution of make failed.  Make command was: "nmake" "/NOLOGO"
"cmTC_b3144\fast"

It seems that the GNU compilers are identified but don't seem to work. Any help would be much appreciated. I am trying to avoid using Cygwin.. but almost ready to go that route in a sec here.

SuperVeetz
  • 2,128
  • 3
  • 24
  • 37
  • 1
    It looks like cmake is configured for Microsoft's toolchain, because it's trying to run `nmake`. – Ben Voigt May 07 '17 at 22:22
  • okay, maybe this is because i'm using Visual Studio Code, i will look into installing nmake. Thanks for the input – SuperVeetz May 07 '17 at 22:29
  • 3
    I would rather suggest telling cmake to use GNU make instead of MS nmake. Try `cmake -G"MinGW Makefiles"` instead of `cmake -G"NMake Makefiles"` See https://cmake.org/Wiki/CMake_Generator_Specific_Information#Makefile_generators – Ben Voigt May 07 '17 at 22:30
  • 2
    Or in VS Code, [set `cmake.generator` as shown here](http://stackoverflow.com/a/42462406/103167), but use `MinGW Makefiles` – Ben Voigt May 07 '17 at 22:35
  • Thank you Ben. It is compiling now. The problem was that I was using `cmake -g "MinGW Makefiles"` and having a lowercase `-g` seemed to cause the problem because when I use uppercase `-G` it works. Good ol windows – SuperVeetz May 07 '17 at 22:37
  • @BenVoigt I'm experiencing an issue where `make` is failing to link `standard c++ libraries`. I have attempted to include the standard libraries in my `c_cpp_properties.json` file for visual studio code as shown here: http://stackoverflow.com/questions/37522462/visual-studio-code-includepath but this did not help. Any ideas? – SuperVeetz May 07 '17 at 23:26
  • Is it linking using `ld`, `gcc`, or `g++`? Usually it's best to set the link tool to `g++` since that will automatically add the options for the C++ libraries and then call `ld`. – Ben Voigt May 07 '17 at 23:49
  • Or maybe you just need to change `gcc.exe` to `g++.exe` in the rules you already showed. – Ben Voigt May 07 '17 at 23:50

1 Answers1

5

To fix the problem I was having, I had to do two things.

  1. use the command cmake -G "MinGW Makefiles" . (capital -G on windows)
  2. update my CMakeLists.txt file to use gcc for C compiler and g++ for C++ compiler

CMakeLists.txt

cmake_minimum_required(VERSION 3.8)

set(CMAKE_C_COMPILER "C:/MinGW/bin/gcc.exe")
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe")

project (Tutorial)
add_executable(Tutorial tutorial.cpp)
SuperVeetz
  • 2,128
  • 3
  • 24
  • 37
  • 2
    Yeah `gcc` and `g++` both run the same compiler, but with different default options. And for the link step, the files are all named `*.o` so the filename provides no hint that C++ libraries should be used. – Ben Voigt May 08 '17 at 00:21
  • 4
    Just FYI, you can avoid setting the variables in the CMakeLists.txt and thus keep your program portable by instead specifying them on the command line `-DCMAKE_C_COMPILER="C:/MinGW/bin/gcc.exe" -DCMAKE_CXX_COMPILER="C:/MinGW/bin/g++.exe"`. – dlasalle May 08 '17 at 02:13