0

Typical cmake produre here was:

cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg.cmake
cmake --build .

That worked on both Windows and macOS. But then I noticed it was building in debug mode.

To choose build mode on Windows I did

cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg.cmake
cmake --build . --config=THEMODE

While on macOS I need to do

cmake .. -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg.cmake -DCMAKE_BUILD_TYPE=THEMODE
cmake --build .

Where THEMODE is either Debug/Release.

I bet I'm missing something, isn't? The Cmake is all about Multiplatform, why it works differently to choose build mode on Windows/macOS?

sepp2k
  • 363,768
  • 54
  • 674
  • 675
KcFnMi
  • 5,516
  • 10
  • 62
  • 136

2 Answers2

1

Most probably, you're generating build files for a multi-configuration tool on Windows (e.g. Visual Studio) and a single-configuration tool on macOS (e.g. Makefile). That's why you need to specify:

paolo
  • 2,345
  • 1
  • 3
  • 17
1

There are multi-config generators and others. "Others" need CMAKE_BUILD_TYPE provided explicitly per cmake configuration. Multi-config can be generated once and built in supported configurations. Visual Studio generator is a multi-config one but it isn't cross-platform. Ninja, on the other hand, is a multi-config generator and cross-platform.

So you generate and build it the same way on all platforms:

cmake -G "Ninja Multi-Config" ..
cmake --build . --config Release

On Windows you need to run this command with VS setup in the terminal aka "Developer Terminal", otherwise there is no Ninja installed by default and MSVC compiler won't be found.

ixSci
  • 13,100
  • 5
  • 45
  • 79
  • Wouldn't it be cool to have `--config` inferring its value from `DCMAKE_BUILD_TYPE`? – KcFnMi Apr 12 '22 at 14:02
  • 1
    @KcFnMi config is an option for multi-config generators and `CMAKE_BUILD_TYPE` is for others. It would not make sense to infer one from the other. Anyway, all of these options will eventually go out of use and will be replaced with CMake presets. – ixSci Apr 12 '22 at 14:33
  • 1
    @KcFnMi there is also [another solution](https://stackoverflow.com/questions/19024259/how-to-change-the-build-type-to-release-mode-in-cmake/19029864#19029864) which will allow you to use only `CMAKE_BUILD_TYPE` everywhere and do not use the `--config` option at all. Many a righteous man don't like that solution, though. As you can clearly see by the score it has. – ixSci Apr 12 '22 at 14:57