3

I have Mingw64 GCC 6.3.0 (always in PATH) and Visual C++ compiler tools from Visual Studio 2017 RTM (not in PATH).

If I run cmake . -G "MinGW Makefiles", GCC 6.3.0 will be selected.

If I run cmake . -G "Ninja", GCC 6.3.0 will be selected.

My Visual C++ compiler tools is none standard, I only keep the parts I need and delete the rest (like MSBuild, IDE etc.). I use my own batch script to set up PATH, INCLUDE and LIB (works just fine).

If I run this batch script and run cmake ., MSVC will be selected and build with NMake.

With the same environment, cmake . -G "Ninja", GCC 6.3.0 is selected instead of MSVC.

So my question is, how to tell CMake I want to use MSVC + Ninja rather than GCC + Ninja when both are in PATH? Any environment variable I should set?

John London
  • 1,250
  • 2
  • 14
  • 32

2 Answers2

6

You can also use the inverted approach and exclude all compilers you don't want with CMAKE_IGNORE_PATH. It takes a list of paths to ignore, but be aware that it needs to be an exact string match. The advantage would be that you can declare those directly from the command line.

So what I did is to take the path from the compiler found but "not to be taken" into CMAKE_IGNORE_PATH.

And on my system there were actually three GCC compilers in my PATH (just make sure to start from an empty binary output directory with each try):

> cmake -G"Ninja" ..
...
-- Check for working C compiler: C:/MinGW/bin/cc.exe
...

> cmake -DCMAKE_IGNORE_PATH="C:/MinGW/bin" -G"Ninja" ..
...
-- Check for working C compiler: C:/Strawberry/c/bin/gcc.exe
...

> cmake -DCMAKE_IGNORE_PATH="C:/MinGW/bin;C:/Strawberry/c/bin" -G"Ninja" ..
...
-- Check for working C compiler: C:/Program Files (x86)/LLVM/bin/clang.exe
...

> cmake -DCMAKE_IGNORE_PATH="C:/MinGW/bin;C:/Strawberry/c/bin;C:/Program Files (x86)/LLVM/bin" -G"Ninja" ..
...
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe
...
Florian
  • 39,996
  • 9
  • 133
  • 149
4

use a toolchain file

set(CMAKE_C_COMPILER cl.exe)
set(CMAKE_CXX_COMPILER cl.exe)

then build your cmake project with with -DCMAKE_TOOLCHAIN_FILE=toolchainfile

MatSch
  • 163
  • 6