在Visual Studio中使用“命令参数”进行调试不起作用

9

我传入的“命令参数”没有在VS中运行。我做错了什么?

尝试在Visual Studio中使用“命令参数”。我将其简化为一个简单的C语言控制台应用程序。

#include "pch.h"
#include <iostream>

int main(int argc, char *argv[])
{
  printf("\nargv: ");
  for (int i = 0; i < argc; i++)
  {
    printf(" %s", argv[i]);
  }
  printf("\n");
}

然后我设置了"命令参数": 项目: 属性: 调试:"命令参数": mom

enter image description here

我点击了“运行”按钮,但没有传递参数,会显示如下内容:

argv:  C:\Users\jack\source\repos\ConsoleApplication45\Debug\ConsoleApplication45.exe

C:\Users\jack\source\repos\ConsoleApplication45\Debug\ConsoleApplication45.exe (process 1812) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

当我从命令行运行时,会得到以下输出(参数"mom"被正确传递):
C:\Users\jack\source\repos\ConsoleApplication45\Debug>ConsoleApplication45.exe mom

argv:  ConsoleApplication45.exe mom

C:\Users\jack\source\repos\ConsoleApplication45\Debug>

感谢Jean-François Fabre提供的链接:
是的,我不得不从x86更改为x64。

enter image description here


1
相关链接:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4097114c-8678-46bb-ba3b-7a2da8514efc/visual-studio-2017-not-passing-command-line-arguments-to-the-application?forum=vsdebug - Jean-François Fabre
你的目标真的是64位吗?上面的链接建议选择“任何CPU”而不是x64。 - Jean-François Fabre
搞定了,Jean-François Fabre。谢谢你。 - jdl
最终程序是x64的。我不习惯在调试器中传递参数。这对我来说是新的。 - jdl
@jdl,很高兴听到您的问题已经解决,请在有空的时候将Jean-François Fabre的回复标记为答案,这将有助于其他人更轻松地搜索到它,谢谢 :) - Sara Liu - MSFT
3个回答

6

4
我认为你在这里的主要问题是解决方案配置/调试属性与实际调试/运行解决方案的平台不匹配。
确保它们对齐。
例如 - 这个配置是针对平台:x64

enter image description here

当运行/调试解决方案时,您需要确保在相同的平台上进行调试:

enter image description here

如果您的平台不是x64,则按照建议配置并在Platform:x86All Platforms下运行该项目。

2

我本以为遇到了类似的问题,但后来发现问题出在属性页对话框的操作上。在早期版本的Visual Studio中,选择一个项目并右键单击“属性”会打开对话框,并将配置设置为当前活动的配置。

但是,在Visual Studio 2017中,这种情况不再适用,它似乎会打开最后编辑的配置,这很容易引起混淆,可能导致某人编辑错误的配置,就像上面所看到的那样。这个变化不太好...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接