I am trying to find an easier way to test my command line application. It has arguments so I run it from a command prompt. My process is this:
Build
Go to the output
Open command prompt
Change directory
Run it
There has to be an easier way not only to debug but to open a command prompt in the current folder.
Thank you for your time.
If you go to the project properties, Debugging settings, you can set the working directory and parameters. If you use the same parameters all of the time, you can enter those in on that screen as well. Then just F5 or Ctrl+F5 to run.
Set a breakpoint at the end of the code to keep it from going away after it is done.
See Debugging with command-line parameters in Visual Studio
Alternatively, you should be able to use a shell script (or Python os.system()) to automate some of those steps.
To open a command prompt in the current directory using explorer, you can shift+right click->Open Command Window Here. That will save a little time.
If you're using Visual Studio, pressing F5 will run the code in the debugger, and Ctrl+F5 will run the code normally. Just remember to include a cin.get() statement at the end or the terminal window will close before you can read the output.
Other IDEs should have similar functions. Check in the Run or Debug menu.
EDIT: Sorry, didn't see that you're asking about running it with arguments. In VS, in Project Properties there are the Debugging settings. Within that, there is a field called Command Arguments. These will get passed to the application when you run it from within VS.
In project properties under debugging you can set the command line arguments (and environment variables) when debugging,
There is an extension called PowerCommands for Visual Studio 2010 that can be installed from Tools -> Extension Manager. It includes a Open Containing Folder and Open Command Prompt functionality that gets added to your right-click menu in the Solution Explorer.
Related
I try to debug my C++ programs but it shows me this error:
Could not find the task 'g++ build active file'.
I tried adding a configuration as Visual Studio Code says in its website, by choosing C/C++: (gdb) Attach, but nothing changes.
I struggled for days to setup a running and debugging environment for C++ in VS Code. Finally I found a way to solve the problem. Please follow the below steps
Prerequisites- Have Mingw32 installed on your PC.
Install C++ extension for VS code
Install Competitive programming helper extension for VS code. Something like
this will appear on the left panel.
Open your .cpp file and open the test runner by clicking on the highlighted are in the above image. Enter the inputs and click on the runall button in the above image you will be able to see the output.
For debugging :-
1.Click on the debug button in the below image . You would get an option stating “Run and Debug file”. Click on that.
It would create a launch.json file and task.json file. Delete the task.json file as we are using competitive programming helper extension for running our cpp files.
Below is the content of launch.json to setup the debugger environment
Paste the same exact configuration mentioned above in your launch.json file.
For the “miDebuggerPath”, please provide the location of the gcc.exe file inside mingw32 folder on your pc.
In the args key I have used "args": ["<", "${fileDirname}\input.txt",">", "${fileDirname}\output.txt"],
The above code is use to take input from input.txt file and write output to output.txt file while debugging for the easy input and output. Please create a input.txt and output.txt in the same folder where you have all your C++ files.
Add a breakpoint in your .cpp file and run the debugger by click on the green RUN button , your debugger would start and it would take all the input from input.txt and write the output in the output.txt after the debugging session ends.
Happy Coding.
Unfortunately, cannot add a comment, so I have to submit an "answer", which is merely a guess. Did you download "mingw64" with g++? Probably, you did not add mingw64/bin to your system path. Control panel -> Edit path -> Environment variables -> Path -> New.. (it is also in the tutorial link below)
To really make sure the things run, check this tutorial, by Microsoft: https://code.visualstudio.com/docs/cpp/config-mingw
Perhaps, uninstall your downloaded g++ and install this mingw64 repository. Quite helpful. I use it myself and for a quick debugging - works like clocks!
In Visual Studio, how can the run command for a console application be viewed?
Imagine a complex C++ application with a lot of startup flags and arguments.
It runs fine from the "Debug -> Start Without Debugging" option from the menu.
I would like to run the executable within a Command Prompt window, without starting Visual Studio. I try and run the executable, without success, like so:
./myProgram.exe
How can the command that VS uses to start the application--with all flags and arguments--be viewed?
If your app is running, Process Explorer shows complete command line. If your app is short lived, Process Monitor records what arguments were used to start it.
I am trying to run a simple C++ program on CLion. The problem is that when I try to compile and run it, it shows the result in the application console. I want to run on the System console like the Visual Studio runs the output of the console apps. Is there a way of doing this. I use MINGW compiler.
You can use an external terminal, do these steps.
(top menu) run\ edit Configurations
tick 'run in external console'
Well, if anyone is still around wanting to open an external cmd window on run, there is a way to do it on Windows as well:
Go to Run > Edit Configurations
For the executable select C:\Windows\System32\cmd.exe
For program arguments use: /c "start cmd.exe #cmd /k "ProjectName.exe""
For working directory set the cmake debug (or release) folder
Save and select the profile from the dropdown right next to the run button
Note: In the third step /k can be replaced with /c to make the window close after the program is run (with /c its more like visual studio and with /k its more like code blocks)
I am not sure about Windows but in linux you can do it using this answer.
You might be able to change the gnome-terminal to cmd if you are using windows but you will probably need to change the "Program Arguments" too.
the above answer works fine in windows also. In working directory you can set this $CMakeCurrentBuildDir$
enter image description here
When I build, compile and run my Visual Studio project, a command prompt also opens with the window. It does this with the compiled .exes as well as in the dev environment. Is there a way to stop the command prompt from opening for the compiled .exes?
You can switch the subsystem in the linker settings from "CONSOLE" to "WINDOWS", which will stop the OS from creating an initial console window for the process. It can still create one afterwards though, in which case you'd have to modify the code.
Try switching compilation mode to 'Release'. Change build target to another than console (depends on your current project, for example Win32 Application).
I'm trying to compile a project from the command line, like this:
devenv.exe myproj.sln /build release
It looks like the code compiles well, but that's not all I need:
I want to be able to capture the output (e.g. warnings, errors) from the compiler as they occur. Unfortunately as soon as I issue the above command I am returned to the command prompt.
When I look at process-explorer or taskmgr.exe I can see that the devenv.exe process (and a few other sub-processes) working away. If I look in the output folder I can see all of my files gradually appearing.
Is there a way of making VCC work a little bit more like GCC - when I issue a build command or make a project using a Makefile, I get a stream of messages and the console blocks until the process has completed.
Update: Thanks, two excellent solutions. I can confirm that it works.
devenv uses this interesting dispatcher that switches between command line mode and windowed mode. There's actually a devenv.com in addition to devenv.exe, and since *.com takes precedence over *.exe, it gets invoked first. devenv.com analyzes the command line and decides what to invoke.
In other words, change your command line to:
devenv myproj.sln /build release
And you should be ok.
use devenv.com in place of devenv.exe and you will get what you want.
You can use MSBuild.exe as well for more options.