# Overriding CMAKE\_CXX\_COMPILER with VS2017 on Windows 10

**URL:** https://discourse.cmake.org/t/overriding-cmake-cxx-compiler-with-vs2017-on-windows-10/1311
**Category:** Code
**Tags:** os:windows, gen:vs
**Created:** [June 3, 2020, 10:25am UTC](https://discourse.cmake.org/t/overriding-cmake-cxx-compiler-with-vs2017-on-windows-10/1311 "2020-06-03T10:25:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![dr-traktor](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dr-traktor/32/664_2.png) [@dr-traktor](https://discourse.cmake.org/u/dr-traktor)
#### Post date: [June 3, 2020, 10:25am UTC](https://discourse.cmake.org/t/overriding-cmake-cxx-compiler-with-vs2017-on-windows-10/1311/1 "2020-06-03T10:25:07Z")

</div>

Is there a way to specify a custom compiler on Windows 10 using Visual Studio  
2017, and not use the one inside the VS2017 installation? Below are my  
attempts to do so.

* * *

Using CMake 3.17.3 on Windows 10 and Visual Studio 2017 I have failed to  
override CMAKE\_CXX\_COMPILER in multiple ways.

1. I used `set(CMAKE_CXX_COMPILER "absolute/path/to/custom/cl.exe")` at the  
top of my CMakeLists.txt file before the `project()` command. The  
`project()` command overwrites the `CMAKE_CXX_COMPILER` to the `cl.exe`  
inside the VS2017 installation. This is the approach I would prefer.
2. I specified `-D CMAKE_CXX_COMPILER=absolute/path/to/custom/cl.exe` at the  
command line to the same effect.

I have the following toy project set up to test this behavior.

```auto
cmake_minimum_required(VERSION 3.15)

set(CMAKE_C_COMPILER "D:/compiler/cl.exe")
set(CMAKE_CXX_COMPILER "D:/compiler/cl.exe")

include(CMakePrintHelpers)
cmake_print_variables(CMAKE_CXX_COMPILER)

project(test)

cmake_print_variables(CMAKE_CXX_COMPILER)

add_executable(hello main.cpp)

```

I was using an out-of-source build and before every attempt I deleted the  
entire build directory.

It does not matter whether I specify “Visual Studio 15 2017” as the generator,  
or let CMake automatically find and select it, the `project` command will  
always overwrite the values in `CMAKE_C_COMPILER` and `CMAKE_CXX_COMPILER`.  
Using cmake-gui or the command line yields the same result.

Using then `cmake --build build --verbose` I can see, that it’s always the  
compiler detected by CMake that is invoked, and not the custom one I am trying  
to use.

---

<div class="post-metadata">

### Author: ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)
#### Post date: [June 3, 2020, 6:55pm UTC](https://discourse.cmake.org/t/overriding-cmake-cxx-compiler-with-vs2017-on-windows-10/1311/2 "2020-06-03T18:55:14Z")

</div>

This was already discussed in this other topic:

> [@VS generator and toolchain files](https://discourse.cmake.org/t/vs-generator-and-toolchain-files/1177/3):
>
> The VS generator produces .vcxproj files and uses MSBuild to actually drive the build. The values of CMAKE\_{C,CXX}\_COMPILER are computed automatically from the compiler MSBuild chooses so setting them has no effect. With this generator we can only support toolchains that have integration with MSBuild installed such that one could manually create a project using them through the IDE. Both the Intel C/C++/Fortran compilers and NVIDIA CUDA compilers are examples of this. For Visual Studio gener…
