Debugging with command-line parameters in Visual Studio

C++Visual StudioDebuggingCommand Line

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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).

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

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.

Solution 5 - C++

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.

Solution 6 - C++

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.

Solution 7 - C++

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

Solution 8 - C++

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.

Solution 9 - C++

In VS 2022 it is possible to debug any executable. Open a folder containing .exe file

  • Right-click on .exe file and click "Set as Startup item"

  • Again right-click on .exe file and click "Add Debug Configuration" or "Open Debug and Launch Settings" if configuration file is already created.

  • Add args to launch.vs.json, e.g.

{
  "version": "0.2.1",
  "defaults": {},
  "configurations": [
    {
      "type": "default",
      "project": "ffprobe.exe",
      "projectTarget": "",
      "name": "ffprobe.exe",
      "args": [ "C:\\Temp\\test-file" ]
    }
  ]
}
  • Right-click on .exe file and click "Debug"

Solution 10 - C++

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.

Solution 11 - C++

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.

Solution 12 - C++

For those using VS2022 and you don't have launchSchema.json, as someone mentioned above, there is a solution for inserting args using launchSettings.json.

enter the file and insert your args using "commandLineArgs": "argument here",

this worked for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaciej GrykaView Question on Stackoverflow
Solution 1 - C++Lou FrancoView Answer on Stackoverflow
Solution 2 - C++grrusselView Answer on Stackoverflow
Solution 3 - C++MSaltersView Answer on Stackoverflow
Solution 4 - C++jorgeView Answer on Stackoverflow
Solution 5 - C++jpmc26View Answer on Stackoverflow
Solution 6 - C++Yahya HusseinView Answer on Stackoverflow
Solution 7 - C++Andrushenko AlexanderView Answer on Stackoverflow
Solution 8 - C++BlubsiwubsiView Answer on Stackoverflow
Solution 9 - C++user1602View Answer on Stackoverflow
Solution 10 - C++sAmView Answer on Stackoverflow
Solution 11 - C++Lars PellarinView Answer on Stackoverflow
Solution 12 - C++MateusLView Answer on Stackoverflow