Search code examples
c++visual-studiovisual-c++cmake

How to Ensure Command-Line Arguments are Applied When Debugging a CMake Project Directly in Visual Studio?


#CMakeLists.txt
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)

project(ArgExample)

add_executable (ArgExample main.cpp)
set_target_properties(ArgExample PROPERTIES VS_DEBUGGER_COMMAND_ARGUMENTS "arg1")
//main.cpp
#include <iostream>

int main(int argc, char* argv[])
{
    std::cout << "Arg example\n\n" << std::endl;

    for (int i = 1; i < argc; i++) {
        std::cout << "Argument " << i << ": " << argv[i] << std::endl;
    }
    
    return 0;
}

I've set up my project as shown in the code above. After creating a .sln file and opening the solution in Visual Studio, I confirmed that everything worked as expected, yielding the correct results.

result (.sln)

However, when I open the CMakeLists.txt file directly in Visual Studio instead of using the solution file to launch a CMake project and start debugging, the arguments are not applied.

result (CMakeLists.txt)

I would like the arguments to be applied even when I use the CMakeLists.txt file directly in Visual Studio. Any advice on how to achieve this would be greatly appreciated.


Solution

  • See Customize debugger settings

    Right lick cmakelists.txt -> add debug configuration

    Add "args": [ "arg1" ] in launch.vs.json