The default startup project generated by <span>cmake</span> is <span>ALL_BUILD</span>. We want to specify the default startup project so that we don’t have to set it every time we open the <span>.sln</span> file.
This was not possible before <span>cmake 3.6</span>. After <span>cmake 3.6</span>, it can be accomplished by setting the <span>VS_STARTUP_PROJECT</span> property.
1. Core Setting Statement
The following statement sets <span>HelloWorld</span> as the startup project instead of the default <span>ALL_BUILD</span> project.
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT HelloWorld)
This line is best added in the top-level CMakeLists.txt. Here, we use <span>${CMAKE_CURRENT_SOURCE_DIR}</span>. If there are multiple CMakeLists.txt files, you can replace <span>${CMAKE_CURRENT_SOURCE_DIR}</span> with <span>${YourProjectName_SOURCE_DIR}</span>.
<span>VS_STARTUP_PROJECT</span> official documentation: https://cmake.org/cmake/help/latest/prop_dir/VS_STARTUP_PROJECT.html
2. A Complete Example
2.1 File Directory
├── CMakeLists.txt
├── generate_sln.bat
└── main.cpp
2.2 CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(HelloWorld)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${ORB_SLAM2_LIBS})
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ${PROJECT_NAME})
2.3 main.cpp
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
getchar();
return 0;
}
2.4 generate_sln.bat Script
Execute cmake to generate the vs project, below is an example for generating a vs 2015 project.
If it is another vs version, modify the following <span>cmake -G "Visual Studio 14 Win64"</span> to your own vs version.
@echo off
setlocal
REM This script is used to generate vs2015 solution files
REM determine if cmake exists
where cmake
if %errorlevel%==0 (
echo cmake found
) else (
echo cmake not found
goto exit_build
)
REM create build directory
set dir_name=build
if exist %dir_name% (
echo delete %dir_name%
rd /s/q %dir_name%
)
md %dir_name%
echo create %dir_name%
cd %dir_name%
REM cmake
cmake -G "Visual Studio 14 Win64" ..
if %errorlevel%==0 (
echo.
echo.
echo solution is generated in the %dir_name%
echo.
echo.
) else (
echo.
echo.
echo solution generating is failed!!! Please fix the problem before cmake.
echo.
echo.
goto exit_build
)
:build_success
pause
exit /b 0
:exit_build
pause
exit /b 1
2.5 Effect After Generating the Project and Opening It
Double-click <span>generate_sln.bat</span>, and it will generate <span>HelloWorld.sln</span> in the <span>build</span> folder.
After opening, the default startup project is as follows, with the default startup project being <span>HelloWorld</span>, no longer the <span>ALL_BUILD</span> project: