Environment variable used by CMake to detect Visual C++ compiler tools for Ninja

核能气质少年 提交于 2019-11-26 07:49:22

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/43069632/environment-variable-used-by-cmake-to-detect-visual-c-compiler-tools-for-ninja

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!