547

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Maciej Gryka
  • 7,553
  • 4
  • 34
  • 29
  • Related post - [Passing command line parameters with Visual Studio C#](https://stackoverflow.com/q/6475887/465053) – RBT May 24 '18 at 10:06

10 Answers10

806

Yes, it's in the Debugging section of the properties page of the project.

In Visual Studio since 2008: right-click the project, choose Properties, go to the Debugging section -- there is a box for "Command Arguments". (Tip: not solution, but project).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Lou Franco
  • 83,503
  • 14
  • 127
  • 183
  • 336
    Stackoverflow is the only usable MS documentation! – Martin Beckett Jan 13 '11 at 16:41
  • 14
    Be careful with that. This will not change the actual project file, but the vcxproj.user-file instead. – TheTrowser May 13 '15 at 17:20
  • Note: if you have multi project in a solution, remember to right click the project you wand to run and "Set as StartUp Project". – Lion Lai Apr 17 '17 at 07:51
  • 2
    Spot on. But apparently in VS2017 it's not called "Debugging", but "Debug". We may never know why. – OmarL Oct 16 '17 at 14:43
  • 4
    Make sure you have the correct Configuration selected in the dropdown at the top of the Property pages, i.e. the same configuration as the one you are trying to run. – Steve Smith Jan 04 '18 at 14:51
  • Command **Arguments**. The actual executable is listed in **Command**. The problem I had was I put the executable in **Command Arguments**...sigh. Thanks for this @lou-franco – Daisuke Aramaki Nov 18 '20 at 18:41
  • @TheTrowser Is it possible to set the arguments from one of the typically version controlled VS files, like .vcxproj? – Bernardo SOUSA Feb 02 '21 at 14:26
  • @BernardoSOUSA Sorry, not working with VS anymore. Perhaps someone else might help you. – DarkTrick Feb 04 '21 at 12:25
58

The Mozilla.org FAQ on debugging Mozilla on Windows is of interest here.

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.

This looks like the following for Visual Studio 8 or 9 (Visual Studio 2005 or Visual Studio 2008, respectively)

  devenv /debugexe 'program name' 'program arguments'

It is also possible to have an explorer action to start a program in the Visual Studio debugger.

grrussel
  • 7,063
  • 6
  • 47
  • 71
19

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.

Lou Franco
  • 83,503
  • 14
  • 127
  • 183
MSalters
  • 159,923
  • 8
  • 140
  • 320
  • 1
    Yep, attach with `Ctrl+Alt+P` (or click "Debug" > "Attach to process..."). But this doesn't really answer OP question ;) – T_D Jan 10 '17 at 12:51
7

Microsoft Visual Studio Ultima 2013.

You can just go to the DEBUG menu → Main PropertiesConfiguration propertiesDebugging and then you will see the box for the command line arguments.

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
jorge
  • 314
  • 3
  • 7
  • It kinda works like that in MS VS 2015 as well. Before I headed to "Debug-> {projectname} properties" I had to open the "Configuration Manager" accessable via the Dropdown containing by default "Debug" and "Release". A window popped up where I was able to add new "Configuration" items. These items are available in "Debug -> {projectname} properties". – AMartinNo1 Jun 11 '16 at 11:36
3

In Visual Studio 2017 with a .NET Core console application do the following:

Right click on the Project in the Solution window, select "Properties", Debug (on the left side), and enter the arguments into the field "Application Arguments".

Note that they should be space-separated.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Yahya Hussein
  • 7,519
  • 12
  • 45
  • 86
3

With VS 2015 and up, Use the Smart Command Line Arguments extension. This plug-in adds a window that allows you to turn arguments on and off:

Smart Command Line Arguments interface

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.

jpmc26
  • 23,237
  • 9
  • 76
  • 129
2

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.

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.

O'Neil
  • 3,638
  • 3
  • 13
  • 28
  • 1
    Had the same issue but both declarations mentioned above didn't work. Solution was changing platform from x86 to x64 since I am working on a 64bit machine. – hfrmobile Mar 06 '20 at 08:18
1

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.

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
sAm
  • 193
  • 1
  • 1
  • 10
1

Right click on the project in the Solution window of Visual Studio, select "Debugging" (on the left side), and enter the arguments into the field "Command Arguments":

Enter image description here

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Andrushenko Alexander
  • 1,273
  • 12
  • 11
0

I found some old command line arguments stored in a MyStartUpProject.csproj.user file under my startup-project's source folder. Deleting this file didnt work, Visual Studio brought it back for me. I had to edit the arguments in the file.

The values in the file did not show up in VS, Project Properties, Debugging. Entering values there appended them to the values in the mysterious MyStartUpProject.csproj.user file.

Lars Pellarin
  • 140
  • 1
  • 5