C++ in Visual Studio 2015: Manually pass in command line args - c++

In visual studio you can specify in
"Project Properties > Debugging > Command Arguments"
the arguments you want to pass in. But I want to be able to change this manually so I can run different arguments and test my code. Is there a way to open the console and pass in arguments like you normally would in a unix shell?
./myProg arg1 arg2
Having to go into the properties and change every time is annoying.
Thanks!

I'm not sure specifically with how you'd like to open the console, like what the intended result here is. I could interpret this a few ways.
You can run your application from an external command prompt set to the binary directory with any args you want, but the issue is that you would have to attach your debugger if you needed to do debugging. You could switch to console-based debugging or you could introduce a console read in order to attach the debugger.
In later versions of Windows, holding shift and right clicking in Explorer yields an Open command window here option. This makes it quick and easy to open up a command prompt. You can right click a C++ project and Open Folder in File Explorer. This'll get you most of the way there.
If you want, you could specify a special sentinel arg that allows you to interactively populate commandline args within the program. Either that or if there are expected to be args and there are none, you could assume that means interactive mode.
Using cmd /c, you might be able to come up with some really tricky things. Using batch files or all sorts of craziness to populate args. You could combine this with build steps etc. to produce output that batch files read. The sky is the limit.
I don't know specifically what sort of thing you were after, but hopefully this enumerates some things that might be of help.

Related

How can I pass commands to the elevated application in a batch file?

I was wondering if anyone can help me. I'm currently working on a game engine project which involves its own c++ compiler. For this I'm "borrowing" the visual studio 2013 c++ compiler. everything works fine. The problem I am having is a cant figure out how I would pass commands to the elevated program in a batch file.
Let me Explain, right now I am using a program which calls the "vcvarsall.bat" file and passes in "x86" as a parameter. This is great for manual entry as it then allows me to input the commands to compile files. E.G "cl /EHsc <cpp files>"
As of now, when I add commands after I call "vcvarsall.bat", they just give me a command reference error saying the command is not recognized.
What I want to achieve is being able to call one bat file which executes and compiles all of my code for me. instead of having to manually type in the commands every time. This way the entire process is easier for the user.
Any help would be appreciated,
Thank you in advance!
when I add commands after I call "vcvarsall.bat"
Maybe it has been too long since I last did a batch file .. hope the following gets you started:
I think any .bat file will accept parameters, and internally, the .bat writer (i.e. you) uses special identifiers. Often they are named something like %1 and %2, etc. (some scripting languages use $1, and probably a few other approaches)
Without consuming these parameters in your .bat file, the command line interpreter tries to use the parameter as another command (so you get 'command not recognized')
Search all .bat files on your system for %1 (or $1 or whatever) ... maybe you'll find enough hints.
Thank you all for the help, the way I solved the problem was by finding the last batch file which was called and making the end of the file call another batch file in the main compile directory, this means I can programatically generate this batch file making it incredibly easy to generate custom compilations. thank you all,
Alister

(VS2010 C++) Execute a command every time the program is run?

The IDE I'm using is VS2010 for writing C++
I want to execute the command cmd C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt every time the program I'm coding is run from within the IDE. (This command should open a console to track changes made to the file Log.txt)
There are ways to make a command run every time the program is built, but I can't find a way to make a command run whenever the program itself is run, even if it's already built. Where or how might I be able to set that kind of thing up?
I've tried putting $(TargetPath) & C:\utilities\unix\tail.exe -f -n15 $(ProjectDir)Log.txt into the project's Properties->Debugging->Command (TargetPath is the full name of the debug executable) but VS reads the entire thing as a filename and gets confused.
You can create a file run.cmd for example next to the vcxproj file, which would contain:
%1
C:\utilities\unix\tail.exe -f -n15 %2Log.txt
And then in Properties->Debugging->Command you write:
$(ProjectDir)\run.cmd
and in Command Arguments you write:
"$(TargetPath)" "$(ProjectDir)"
I may have misspelled the macros, but you get the idea: it executes first your program and then whatever you want.
Edit: Unfortunately it works only if you start without debugging (Ctrl+F5), because otherwise the debugger tries to attach to run.cmd and complains that the format is unsupported.

Compiled exe won't run through my custom schemed cmd?

I have a custom color/font/size/etc. scheme for my cmd.
When I run my program through VS (with or without debugging) it runs my program in cmd using my scheme. (good)
But, when I run the exe file directly, it runs in the default cmd (without my scheme). (bad)
If I make a .bat file which executes my exe, then it runs in the schemed cmd. (good)
But if I make a shortcut of that .bat file, it runs in the default cmd! (bad)
How can I make it so that the exe itself runs through the schemed cmd?
If not possible, how can I make it so the shortcut to the exe/bat runs it through the schemed cmd?
If you want the software to run in schemed cmd, you should execute the program over command line, other way you can edit your program's font etc. in your program.
Compilers run programs by cmd.
And if you want it so much, you can check your program on task manager at beginning your code and if there isn't, you can run with system function and hide first one.
I don't know how you changed your cmd scheme, so, it is possible that this will not help you. Anyway...
The standard way of cmd customization is to include in HKCU\Console\ a subkey with the title of the console that will be customized, and inside this key, the values that define the console properties (more here)
So, let's say you have now a console scheme in the registry. When you start a console program via a shortcut, the window title of the started console is the text indicated as the shortcut name. Match shortcut name to console name defined in registry (or the reverse).

File as Command Line arguments

I am new to C++ and I am learning with Visual Studio.
I want to make a small program that reads a text from the command line and opens the text. I know that my program has to start like this:
int main (int argc, char *argv[] ){
ifstream File( argv[1] )
But I am super confused on how to run it from the Visual Studio Command Prompt. I know there are a lot of questions like this but I still haven't found what I am looking for. I read that you have to go to properties, Debug and change the command line arguments, but what exactly do I need to put in there? And what should I type in the VS Command Prompt.
Thank you!
From menu find: Project->Properties. Or from the Solution Explorer tree right click on the project and select Properties.
Now, in the opened dialog left pane select: Configuration Properties->Debugging
Then in the right pane grid find the line titled: Command Arguments
Fill it with the input file name (I think you better put it there as a full path, if there is a space in the path use with double quotas. like this:
Without space:
filepathwithoutspace.txt
or with spaces:
"file path with spaces.txt"
Good luck.
For starters, your code should not start like that: before
passing argv[1] to std::ifstream::ifstream, you should
verify that there is an argument, and output an error message
otherwise. As it is, you could end up passing a null pointer to
the constructor of ifstream, which will result (normally) in
a program crash.
As to how to run it: where did you put the executable? If
you're in the Visual Studio Command Prompt window, and have
invoked cl, then by default, the executable should be in the
local directory. Just enter .\name, where name is the name
of your program. If you've actually compiled it from within the
IDE, then in the command window, you should use cd to navigate
to where the executable was generated (which you can find out
from your properties), and invoke it as above; or you can simply
specify the path completely:
c:\Users\me\whereeverIPutTheThing\name.
If you want to debug (using the debugger), you need to specify:
1) the name of the executable (but the default should be good),
2) the parameters to pass it (what you want to see in
argv[1]—don't forget the quotes if it has a space in
it), and 3) the directory where the executable should run. The
second and third are somewhat interdependant: you can, for
example, specify just the filename in 2, and the path where the
file is located in 3, or you can specify the complete path to
the filename in 2, and forget about 3. Or use a combination of
the two: in practice, I tend to do everything from the root
directory of the project, so I'd specify a path relative to this
directory, and then the path from my project file to this root
in 3. (The way we have things set up, this is ..\..\.., but
I think you'll find it somewhat shorter.)

anyone know how to add command line args in vs2008

i have a program that runs like so:
a.out 23421232
now if i use a.out it will tell me check params and gives an example and closes.
I am curious if there is a way to add command line args when executing my code in vs2008?
Right click the project in Visual Studio. Click Properties. On the Debugging page, there is a Command Arguments property.
Besides using the VS IDE to add parameters for running the program in the IDE, you can also open a command prompt window (Start | Run | cmd) and run the program the same as in Linux, except the .exe extension is optional:
C:\Windows> cd "\Documents and Settings\Administrator\Applications\MyProject"
C:\Documents and Settings\Administrator\Applications\MyProject> myprogram 23421232
VS doesn't normally produce an executable named a.out like most Unix compilers do. Instead, given an input XXX.cpp, it'll produce an executable named XXX.exe.
Adding command line arguments is done by bringing up the project properties (Alt+F7), selecting "Debugging" and then entering the argument(s) in the "Command Arguments" control. There, you'll add JUST the argument "23421232" (or whatever).
Go into the Project Properties window for your executable project.
Under the "Debug" section, you can specify your command line arguments. These will be used when debugging.