在Visual Studio中使用“命令参数”进行调试无法正常工作

时间:2019-01-14 20:29:46

标签: c debugging visual-studio-2017 commandargument

VS无法与传入的“命令参数”一起运行。我在做什么不对?

尝试在Visual Studio中使用“命令参数”。
我将其分解为一个简单的C语言小型ConsoleApplication程序。

#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

我按下了RUN按钮,并且显示了此消息(我的参数未传递):

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çoisFabre:
是的,我不得不从x86更改为x64。

enter image description here

3 个答案:

答案 0 :(得分: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中所述,您只需要为x64目标设置参数

您可能正在运行x86,而参数为空。

最好的方法是将它们设置为“ Any CPU”,因为根据目标处理器的不同,这种可能性很小。

答案 1 :(得分:0)

我认为您的主要问题是解决方案配置/调试属性与实际在哪个平台上调试/运行该解决方案不匹配。

确保它们对齐。

示例-此配置适用于Platform: x64

enter image description here

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

enter image description here

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

答案 2 :(得分:0)

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

对于Visual Studio 2017,情况已不再如此,似乎使用上一次编辑的配置打开,这令人困惑,并且可能导致某人编辑错误配置的设置,如上所示。不好的变化...