ialkeilani
ialkeilani

Reputation: 11

Simple C++ program builds ok in CMake with Debug Config, but fails with release

I have this simple C++ program:

#include <iostream>
int main(int argc, char *argv[])
{
   std::cout << "Hello Build Type!" << std::endl;
   std::cin.get();
   return 0;
}

I can build it with no issues using CMake command: cmake --build .

with CmakeLists.txt being this:

cmake_minimum_required(VERSION 3.5)
set(CMAKE_CONFIGURATION_TYPES DEBUG)

project (build_type CXX)
add_executable(cmake_examples_build_type main.cpp)

However, when I change CMAKE_CONFIGURATION_TYPES to RELEASE, I get this error when I build with cmake --build .

Build started 10/24/2018 10:35:38 AM.
Project "C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ALL_BUILD.vcxproj" on node 1 (default targets).
Project "C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ALL_BUILD.vcxproj" (1) is building
"C:\Users\ialkeilani\OneDrive
- Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ZERO_CHECK.vcxproj" (2) on node 1 (default targets).
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets(61,5): error MSB8013: This project doesn't contain the Configuration and Platform combination of Debug|Win32. [C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ZERO_CHECK.vcxproj] Done Building Project "C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ZERO_CHECK.vcxproj" (default targets) -- FAILED.

Done Building Project "C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ALL_BUILD.vcxproj" (default targets) -- FAILED.

Build FAILED.

"C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ALL_BUILD.vcxproj" (default target) (1) -> "C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ZERO_CHECK.vcxproj" (default target) (2) -> (PlatformPrepareForBuild target) -> C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets(61,5): error MSB8013: This project doesn't contain the Configuration and Platfor m combination of Debug|Win32. [C:\Users\ialkeilani\OneDrive - Ushrauto\temp\cmake-examples-master\01-basic\F-build-type\build\ZERO_CHECK.vcxproj]

0 Warning(s)
1 Error(s)

Time Elapsed 00:00:00.18

Moreover, when I attempt to build using MSVC with this command: msbuild build_type.sln /p:configuration=release

build finishes ok with no issues.

Can someone please enlighten me as to why is CMake is failing to build the release configuration while MSVC has no issues?

Running MS Visual Studio 2013 and CMake 3.5.2

Upvotes: 1

Views: 1920

Answers (2)

user10871415
user10871415

Reputation:

Visual Studio is a "multi configurations" generator, hence:

Setting CMAKE_BUILD_TYPE has no effect. You can verify this by opening the generated solution: All configurations are still available.

Setting CMAKE_CONFIGURATION_TYPES=Release is the correct way: Opening the generated solution in the IDE will only allow you to select the "Release" configuration. Building you example will work just fine from within the Visual Studio IDE.

However there seems to be a bug in CMake in combination with Visual Studio Generator when running cmake --build . Changing the command to cmake --build . --config Release will work. This seems to be a regression in CMake, as this used to work at some point. I ran into this specific issue myself after recently upgrading my build environment.

Btw this really seems to be only happening with Visual Studio. I didn't see this problem with XCode, which is also a "multi configurations" generator.

Upvotes: 2

Gergely Nyiri
Gergely Nyiri

Reputation: 345

You should use below option instead of CMAKE_CONFIGURATION_TYPES:

CMAKE_BUILD_TYPE

This can be set either through cmake:

cmake -DCMAKE_BUILD_TYPE=Debug <cmake-source-tree>

or by using ccmake:

ccmake <cmake-source-tree>

enter image description here

Note that you need to configure first (as shown above) and then do the build step. That is:

cmake -DCMAKE_BUILD_TYPE=<Release|Debug> <cmake-source-tree>
make

. if you do your build in your project path, but consider using out-of-source build as proposed by CMake.

Upvotes: -1

Related Questions