Setting Default Startup Project in CMake-Generated Visual Studio Solutions


VS_STARTUP_PROJECT

In CMake, the VS_STARTUP_PROJECT property is used within a CMakeLists.txt file to specify the default startup project for a Visual Studio solution (.sln) generated by CMake. This property is particularly helpful for projects with multiple executables or libraries, allowing you to designate the one you want to build and run first when opening the solution in Visual Studio.

How it Works

    • Within your CMakeLists.txt file, use the set_property command to set the VS_STARTUP_PROJECT property.

    • The command syntax is:

      set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT <target_name>)
      
    • Replace <target_name> with the actual name of the executable target you want to be the startup project.

  1. CMake Generation

    • When you run CMake to generate the Visual Studio project files, it considers the VS_STARTUP_PROJECT property.
    • CMake modifies the generated solution file (.sln) to list the specified target as the first project in the solution.
  2. Visual Studio Behavior

    • When you open the generated solution in Visual Studio for the first time, Visual Studio will automatically select the project listed first in the solution file (the one you designated with VS_STARTUP_PROJECT) as the startup project. This means that project will be built and run by default when you initiate debugging or execution commands within Visual Studio.

Important Considerations

  • Cross-Platform Compatibility
    While VS_STARTUP_PROJECT is primarily used for Visual Studio solutions, it doesn't affect the build process itself. It simply configures the generated solution file for Visual Studio's project management.
  • Target Name
    The <target_name> you provide in the set_property command must match an existing target defined in your CMake project (typically created using the add_executable or add_library commands).
  • CMake Version Requirement
    This VS_STARTUP_PROJECT property is only supported in CMake versions 3.6 and later. If you're using an older version, you won't have this functionality.

Relationship to "Properties: Directories"

The VS_STARTUP_PROJECT property doesn't directly interact with the "Properties: Directories" section in Visual Studio's project properties. "Properties: Directories" is used to configure various source code and build-related paths for your project, such as include directories, source directories, and library directories. VS_STARTUP_PROJECT focuses solely on setting the initial startup project within the generated Visual Studio solution.



cmake_minimum_required(VERSION 3.6)  # Ensure CMake 3.6 or later for VS_STARTUP_PROJECT

project(MyProject)

# Define executables
add_executable(my_executable main.cpp)
add_executable(another_executable secondary.cpp)

# Set my_executable as the startup project
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT my_executable)
  1. CMake Version Check
    We start with cmake_minimum_required(VERSION 3.6) to ensure we're using CMake 3.6 or later, as this property is not supported in older versions.
  2. Project Definition
    We define a project named MyProject using project().
  3. Executable Targets
    We create two executables, my_executable and another_executable, using add_executable.
  4. Startup Project
    We use set_property to set the VS_STARTUP_PROJECT property for the current directory (${CMAKE_CURRENT_SOURCE_DIR}). The second argument (my_executable) specifies that my_executable will be the default startup project in the generated Visual Studio solution.


Manual Selection within Visual Studio

  • This is the simplest method, but it requires manual intervention each time you open the solution.
    • Open the generated solution (.sln) file in Visual Studio.
    • In the Solution Explorer window, right-click on the desired project (e.g., my_executable).
    • Select "Set as Startup Project".

Post-Generation Script (Limited Usefulness)

  • This approach involves creating a script that modifies the generated solution file (.sln) after CMake finishes. This is a less straightforward method and requires scripting knowledge.
    • Generate the Visual Studio project files using CMake.
    • Write a script (e.g., Bash, Python) that parses the generated solution file (.sln).
    • Within the script, modify the order of project lines to place the desired project (e.g., my_executable) as the first project in the list.
    • Execute the script after CMake generation to automatically rearrange the project order.

Important Note

  • The second method of using a post-generation script can be fragile and may not work consistently across different versions of Visual Studio or CMake. It's generally recommended to stick with VS_STARTUP_PROJECT for simplicity and reliability.
  • If you have a complex requirement for managing startup projects, consider writing a custom script or integrating with your build system for more control.
  • These alternative methods are not platform-specific and can be used even when targeting non-Visual Studio environments.