How to explicitly set msvc toolchain in conan profile

121 Views Asked by At

I'm testing my new library on windows and I want to make sure it also builds with MSVC. Now I say I want to test with multiple versions of msvc and I also have mingw and cygwin versions of gcc installed, how do I explicitly select the msvc compilers in a conan profile?

On Linux I would do something like this:

 [settings]
arch=x86_64
build_type=Release
compiler=gcc
compiler.version=11
compiler.cppstd=20
compiler.libcxx=libstdc++11
os=Linux

[conf]
tools.build:compiler_executables={"cpp": "/usr/bin/g++-11", "c": "/usr/bin/gcc-11"}

But on Windows this seems to be a bit harder, since this overview states that users should explicitly set the buildenv via a script to get all the right environment variables right. I'm not sure to what extent this is actually necessary, but I'm also not too familiar with msvc. I'm using a cmake buildsystem btw.

1

There are 1 best solutions below

0
On

If you have CMake and Visual Studio installed, all that is necessary is:

$ conan profile detect

Will detect the default profile using msvc compiler. This profile is good enough to be used to compile a CMake project. You can test it with:

$ conan new cmake_lib -d name=mypkg -d version=0.1
$ conan create .

Conan and CMake manage to setup everything that is needed to do the right build. If you have different VS versions installed, just changing the version will do it too:

$ conan create . -s compiler.version=192

Where 193=VS 2022 (v143 toolset), 192=VS 2019 (v142 toolset), etc.

Compiling with Windows subsystems can require more configuration. Furthermore be aware that packages in ConanCenter are built in Windows with msvc only, that means they are not necessarily tested with MinGW or Cygwin, so some packages might fail to build/work in those subsystems.

It also depends which MinGW are you using, if the one in Msys2, or standalone. For building with them, the approach is:

  • Define your own profile file with os=Windows, compiler=gcc, compiler.version=<yourversion>, compiler.libcxx=libstdc++11
  • If you already have MinGW (including mingw32-make) and gcc in the path, then nothing else would be necessary. If they are not, you might want to add them to the PATH+=(path)/path/to/mingw/bin in the profile [buildenv] section.