Adding command line arguments to project

Why so complicated? VS and CMake support this out of the box as described here. Open the CMakeLists.txt file in VS, and open the "Debug and launch settings" (e.g. via the "CMake" menu or via right-click in the "Solution Explorer". E.g.:

enter image description here

Add your program arguments to "args" in the file "launch.vs.json" which pops up.

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "tests\\hellotest",
      "name": "tests\\hellotest with args",
      "args": ["argument after argument"]
    }
  ]
}

This answer was written before CMake 3.13 was released, so it's now outdated. There is an answer showing solution with CMake 3.13+ also available.


CMake 3.12 and below has no built-in support for this. The reason is that the settings from the Debugging tab of Visual Studio Project properties are not stored in the project file (.vc[x]proj), but in a user-and-machine-specific .user file, and CMake does not generate these.

You can code it yourself in CMake (I did that for our framework at work). The file is just XML, so you can pre-populate it according to your needs. Its structure is pretty easy to understand. The command-line arguments for the program being debugged are stored in the CommandArguments attribute inside a <DebugSettings> XML element (nested in <Configurations><Configuration>), for example.


CMake 3.13.0 looks like it will add support for this in the form of the following target properties:

  • VS_DEBUGGER_COMMAND_ARGUMENTS - Sets the local debugger command line arguments for Visual Studio C++ targets.
  • VS_DEBUGGER_ENVIRONMENT - Sets the local debugger environment for Visual Studio C++ targets.

It extends use with these commands, available since CMake 3.12:

  • VS_DEBUGGER_COMMAND - Sets the local debugger command for Visual Studio C++ targets.
  • VS_DEBUGGER_WORKING_DIRECTORY - Sets the local debugger working directory for Visual Studio C++ targets.

Tags:

Cmake