C++ 在 Visual Studio 中使用命令行参数进行调试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/298708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debugging with command-line parameters in Visual Studio
提问by Maciej Gryka
I'm developing a C++ command-line application in Visual Studio and need to debug it with command-line arguments. At the moment I just run the generated EXE file with the arguments I need (like this program.exe -file.txt
) , but this way I can't debug. Is there somewhere I can specify the arguments for debugging?
我正在 Visual Studio 中开发 C++ 命令行应用程序,需要使用命令行参数对其进行调试。目前我只是使用我需要的参数运行生成的 EXE 文件(像这样program.exe -file.txt
),但这样我无法调试。有什么地方可以指定调试参数吗?
回答by Lou Franco
Yes, it's in the Debuggingsection of the properties page of the project.
是的,它位于项目属性页面的调试部分。
In Visual Studio since 2008: right-click the project, choose Properties, go to the Debuggingsection -- there is a box for "Command Arguments". (Tip: not solution, but project).
自 2008 年以来在 Visual Studio 中:右键单击项目,选择Properties,转到Debugging部分 - 有一个“Command Arguments”框。(提示:不是解决方案,而是项目)。
回答by grrussel
The Mozilla.org FAQ on debugging Mozilla on Windowsis of interest here.
在Mozilla.org的常见问题在Windows调试Mozilla的是在这里讨论。
In short, the Visual Studio debugger can be invoked on a program from the command line, allowing one to specify the command line arguments when invoking a command line program, directly on the command line.
简而言之,Visual Studio 调试器可以从命令行在程序上调用,允许在调用命令行程序时直接在命令行上指定命令行参数。
This looks like the following for Visual Studio 8 or 9 (Visual Studio 2005 or Visual Studio 2008, respectively)
对于 Visual Studio 8 或 9(分别为 Visual Studio 2005 或 Visual Studio 2008),如下所示
devenv /debugexe 'program name' 'program arguments'
It is also possible to have an explorer actionto start a program in the Visual Studio debugger.
还可以使用资源管理器操作在 Visual Studio 调试器中启动程序。
回答by MSalters
Even if you do start the executable outside Visual Studio, you can still use the "Attach" command to connect Visual Studio to your already-running executable. This can be useful e.g. when your application is run as a plug-in within another application.
即使您确实在 Visual Studio 之外启动了可执行文件,您仍然可以使用“附加”命令将 Visual Studio 连接到您已经运行的可执行文件。例如,当您的应用程序作为另一个应用程序中的插件运行时,这会很有用。
回答by jorge
Microsoft Visual Studio Ultima 2013.
Microsoft Visual Studio Ultima 2013。
You can just go to the DEBUG menu → Main Properties→ Configuration properties→ Debuggingand then you will see the box for the command line arguments.
您可以转到 DEBUG 菜单 → Main Properties→ Configuration properties→ Debugging,然后您将看到命令行参数框。
Actually, you can set the same input arguments for all the different configurations and not only for debugging.
实际上,您可以为所有不同的配置设置相同的输入参数,而不仅仅是为了调试。
From the pull down menu of configuration select: All Configurations and insert the input arguments (each argument separated by space).
从配置的下拉菜单中选择:所有配置并插入输入参数(每个参数用空格分隔)。
Now, you can execute your program in different modes without having to change the input arguments every time.
现在,您可以在不同的模式下执行您的程序,而不必每次都更改输入参数。
回答by Yahya Hussein
In Visual Studio 2017 with a .NET Coreconsole application do the following:
在带有.NET Core控制台应用程序的Visual Studio 2017 中,执行以下操作:
Right click on the Project in the Solutionwindow, select "Properties", Debug(on the left side), and enter the arguments into the field "Application Arguments".
右键单击“解决方案”窗口中的“项目”,选择“属性”、“调试”(在左侧),然后在“应用程序参数”字段中输入参数。
Note that they should be space-separated.
请注意,它们应该以空格分隔。
回答by Blubsiwubsi
This may help some people who still have problems.
I use Visual Studio 2015 and I could only pass the arguments when I changed the definition of argv
.
这可能会帮助一些仍然有问题的人。我使用 Visual Studio 2015 并且我只能在更改argv
.
Instead of
代替
int main(int argc, char **argv){
}
I had to use
我不得不使用
int main(int argc, char *argv[]){
}
I do not know why it was necessary, but it works.
我不知道为什么它是必要的,但它有效。
回答by jpmc26
With VS 2015 and up, Use the Smart Command Line Argumentsextension. This plug-in adds a window that allows you to turn arguments on and off:
对于 VS 2015 及更高版本,使用智能命令行参数扩展。此插件添加了一个窗口,允许您打开和关闭参数:
The extension additionally stores the arguments in a JSON file, allowing you to commit them to source control. In addition to ensuring you don't have to type in all the arguments every single time, this serves as a useful supplement to your documentation for other developers to discover the available options.
该扩展还将参数存储在 JSON 文件中,允许您将它们提交到源代码控制。除了确保您不必每次都输入所有参数之外,这还可以作为您文档的有用补充,以便其他开发人员发现可用选项。
回答by Andrushenko Alexander
回答by sAm
In Visual Studio 2010, right click the project, choose Properties, click the configuring properties section on the left pane, then click Debugging, then on the right pane there is a box for command arguments.
在 Visual Studio 2010 中,右键单击项目,选择Properties,单击左窗格中的配置属性部分,然后单击Debugging,然后在右窗格中有一个命令参数框。
In that enter the command line arguments. You are good to go. Now debug and see the result. If you are tired of changing in the properties then temporarily give the input directly in the program.
在其中输入命令行参数。你已准备好出发。现在调试并查看结果。如果您厌倦了更改属性,请暂时直接在程序中输入。