简体   繁体   English

如何在 MSVC 2019 的 CMake 管理的 C++ 项目中将命令行启动 arguments 添加到发布目标?

[英]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.我的目标是从 MSVC 运行例如MyTarget.exe "C:\Users\MHebes\config.json

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:如果我右键单击我的顶级 CMakeLists.txt,我可以看到当前打开文件夹的launch.vs.json

..\..\..\..\AppData\Local\Microsoft\VisualStudio\16.0_45505961\OpenFolder\launch_schema.json : ..\..\..\..\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.首先我并不完全理解这一点,因为launch.vs.json 文档没有提到任何关于"configurations"列表的内容,但这就是我添加新配置时自动填充的方式。 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.无论如何,这在调试模式下有效——“调试 MyTarget 的启动目标”选项显示在Select Startup Item...列表中,当在下拉列表中选择Debug时。

I have added a Release / RelWithDebugInfo configuration to my CMakeSettings.json .我在CMakeSettings.json中添加了Release / RelWithDebugInfo配置。

But when I actually switch the build to Release , the Select Startup Item... list is only populated with default CMakeTargets.但是当我实际将构建切换到Release时, Select Startup Item...列表仅填充了默认的 CMakeTargets。

How do I add command-line launch arguments to Release builds?如何将命令行启动 arguments 添加到发布版本?

My end goal is that when I Start Debugging in Release mode, it will build a Release exe and run it with some args.我的最终目标是,当我在发布模式下开始调试时,它将构建一个发布 exe 并使用一些参数运行它。

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: 配置 CMake 调试会话上的 Microsoft Docs 页面说:

projectTarget : Specifies the CMake target to invoke when building the project. projectTarget :指定在构建项目时要调用的 CMake 目标。 Visual Studio autopopulates this property if you enter launch.vs.json from the Debug Menu or Targets View.如果您从“调试”菜单或“目标”视图输入 launch.vs.json,Visual Studio 会自动填充此属性。 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.由于 Visual Studio 使用包含可执行文件路径的名称填充启动列表,并且因为该路径取决于配置名称(即buildRoot中的CMakeSettings.json默认在路径中包含${name} ),这解释了为什么projectTarget必须包含配置路径。

This seems like an insane interface to me.这对我来说似乎是一个疯狂的界面。 Maybe I'm doing something wrong.也许我做错了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 VSCode 中,无法在 launch.json 中使用 args 将命令行 arguments 传递给 C++ 项目使用 Z272CEADB84586029CCZ4AEB30458602 - In VSCode, unable to use args in launch.json to pass command-line arguments to C++ project using cmake CMAKE - 将默认命令行参数添加到cmake项目 - CMAKE - Add default command line arguments to a cmake project 如何在 VS 2019 中将 Tesseract 链接到 C++ 项目? - How do I link Tesseract to a C++ project in VS 2019? 无法使用命令行参数 C++ 启动子进程 - Unable to Launch Child Process with command line arguments C++ 如何使用 CMake 搭建新的 C++ 项目? - How do I scaffold a new C++ project using CMake? 如何创建将命令行参数传递给 shell 脚本的 C++ 程序? - How do I create a C++ program that passes command line arguments to a shell script? 如何将自定义生成目标添加到Visual C ++ 2010项目? - How do I add a custom build target to a Visual C++ 2010 project? 如何从托管代码项目中调试本机代码项目? C++/C# - How do I debug a native code project from inside a managed code project? C++/C# 如何在命令行上构建Visual Studio 2008 C ++项目的所有配置? - How do I build all configurations of a Visual Studio 2008 C++ project on the command line? Cmake 托管 C++ - Cmake managed C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM