Knowledge.ToString()

CMake – How to Generate Visual Studio Project

You are a developer familiar with Visual Studio on Windows. If you have downloaded C/C++ code from Github, you will find it intimidating to build your code with unknown tools. Your primary goal is to quickly compile the code on a click of a button and not worry about compilation details behind the scene.

C/C++ projects available on Github is using cmake but it is difficult to follow the compilation process. The easiest solution is to create a Visual Studio solution/project and compile it.

Prerequisites for Generating Visual Studio Solution

In order to generate Visual Studio solution, you must have following installed.

  1. Visual Studio “Desktop Development with C++” component. You need to go to Visual Studio installer and select/install that component
  2. CMake

Generate Visual Studio Solution using CMake

If you have downloaded AwesomeSoftware source code in C:\temp001\AwesomeSoftware, it is expected that CMakeLists.txt file is available at C:\temp001\AwesomeSoftware\CMakeLists.txt. You need to create a folder “VisualStudioBuild” (or name of your choice) under C:\temp001\AwesomeSoftware\ and open the Command Prompt.

Use following command to navigate to folder

cd "C:\temp001\AwesomeSoftware\VisualStudioBuild"

Use following command to generate Visual Studio 2022 solution for Windows 64. If you have other version of Visual Studio, you need to change the version in command.

cmake .. -G "Visual Studio 17 2022" -A x64

Visual Studio solution (.sln) file will be generated in C:\temp001\AwesomeSoftware\VisualStudioBuild. Open the solution and compile the project. It is possible that it may not fully compile the project due to how include library works but that is the next step. At least you have a good start.

Based on internet search I had used different commands to generate Visual Studio solution but it gave following error so don’t use those commands.

C:\temp001\AwesomeSoftware\VisualStudioBuild>cmake .. -G "Visual Studio 17 2022" -A Win64
-- Setting policy CMP0091 to NEW
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22621.
CMake Error at CMakeLists.txt:48 (project):
  Failed to run MSBuild command:

    C:/Program Files/Microsoft Visual Studio/2022/Community/MSBuild/Current/Bin/amd64/MSBuild.exe

  to get the value of VCTargetsPath:

    MSBuild version 17.5.1+f6fdcf537 for .NET Framework
    Build started 4/20/2023 11:23:26 AM.
    Included response file: C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\MSBuild.rsp

    Project "C:\temp001\AwesomeSoftware\VisualStudioBuild\CMakeFiles\3.26.3\VCTargetsPath.vcxproj" on node 1 (default targets).
    C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(832,5): error : The BaseOutputPath/OutputPath property is not set for project 'VCTargetsPath.vcxproj'.  Please check to make sure that you have specified a valid combination of Configuration and Platform for this project.  Configuration='Debug'  Platform='Win64'.  You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn't exist for this project. [C:\temp001\AwesomeSoftware\VisualStudioBuild\CMakeFiles\3.26.3\VCTargetsPath.vcxproj]
    Done Building Project "C:\temp001\AwesomeSoftware\VisualStudioBuild\CMakeFiles\3.26.3\VCTargetsPath.vcxproj" (default targets) -- FAILED.

    Build FAILED.

    "C:\temp001\AwesomeSoftware\VisualStudioBuild\CMakeFiles\3.26.3\VCTargetsPath.vcxproj" (default target) (1) ->
    (_CheckForInvalidConfigurationAndPlatform target) ->
      C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(832,5): error : The BaseOutputPath/OutputPath property is not set for project 'VCTargetsPath.vcxproj'.  Please check to make sure that you have specified a valid combination of Configuration and Platform for this project.  Configuration='Debug'  Platform='Win64'.  You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn't exist for this project. [C:\temp001\AwesomeSoftware\VisualStudioBuild\CMakeFiles\3.26.3\VCTargetsPath.vcxproj]

        0 Warning(s)
        1 Error(s)

    Time Elapsed 00:00:00.41


  Exit code: 1



-- Configuring incomplete, errors occurred!

Share

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *