简体   繁体   中英

How do I add command line launch arguments to a Release target in a CMake-managed C++ project in MSVC 2019?

My goal is to run eg MyTarget.exe "C:\Users\MHebes\config.json from MSVC.

I can do this in Debug mode, but can't get it to work in Release.

If I right-click on my top-level CMakeLists.txt, I can see the launch.vs.json for the current open folder:

..\..\..\..\AppData\Local\Microsoft\VisualStudio\16.0_45505961\OpenFolder\launch_schema.json :

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "MyTarget.exe (apps\\MyTarget\\Debug\\MyTarget.exe)",
      "name": "Debug launch target for MyTarget",
      "args": [
        "C:/Users/MHebes/config.json"
      ]
    }
  ]
}

I don't fully understand this in the first place, since the launch.vs.json docs don't mention anything about a "configurations" list but this was how it was auto-populated when I added a new configuration. Regardless, this works in debug mode—the "Debug launch target for MyTarget" option shows up in the Select Startup Item... list when Debug is selected in the dropdown.

I have added a Release / RelWithDebugInfo configuration to my CMakeSettings.json .

But when I actually switch the build to Release , the Select Startup Item... list is only populated with default CMakeTargets.

How do I add command-line launch arguments to Release builds?

My end goal is that when I Start Debugging in Release mode, it will build a Release exe and run it with some args.

I think I figured it out. I added another configuration with a different target:

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "MyTarget.exe (apps\\MyTarget\\Debug\\MyTarget.exe)",
      "name": "Debug launch target for MyTarget",
      "args": [
        "C:/Users/MHebes/config.json"
      ]
    },
    {
      "type": "default",
      "project": "CMakeLists.txt",
      "projectTarget": "MyTarget.exe (apps\\MyTarget\\RelWithDebInfo\\MyTarget.exe)",
      "name": "RelWithDebInfo launch target for MyTarget",
      "args": [
        "C:/Users/MHebes/config.json"
      ]
    }
  ]
}

The Microsoft Docs page on Configure CMake debugging sessions says:

projectTarget : Specifies the CMake target to invoke when building the project. Visual Studio autopopulates this property if you enter launch.vs.json from the Debug Menu or Targets View. This value must match the name of an existing debug target listed in the Startup Item dropdown.

Since Visual studio populates the Startup list with names that include the path to the executable, and because that path is dependent on the configuration name (ie buildRoot in CMakeSettings.json includes ${name} in the path by default), this explains why projectTarget must include the configuration path.

This seems like an insane interface to me. Maybe I'm doing something wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM