How do I run C++ program in Powershell, just like CMD?
19,159
Solution 1
It is a best practice to use the invocation operator and to quote the command.
& ".\a.exe" p1 p2 p3
PowerShell will also allow the use of /
as the path separator.
& "./a.exe" p1 p2 p3 "p4 with space"
Solution 2
by running g++ filename.cpp on PowerShell it generates a.exe file by default on running a.exe it gives an error
so follow these commands
g++ filename.cpp -o filename.exe
filename.exe
Author by
Vrishank Gupta
Updated on June 04, 2022Comments
-
Vrishank Gupta almost 2 years
Like in CMD, to run a C++ program, I use the command
g++ filename.cpp
, then I run it using the commanda.exe
, which opens the output in the CMD itself. How to do such thing using a PowerShell? I am unable to open the file by simple command asa.exe
. Am I doing it the wrong way?-
user4581301 almost 7 yearsTo be honest I've been wimping out. I run cmd in powershell, then do the old cmd stuff the cmd way.
-
BLUEPIXY almost 7 yearsTry like this
./a
-
Eryk Sun almost 7 yearsRunning a.exe from CMD does not output to "CMD itself". cmd.exe, powershell.exe, and a.exe are console applications that will either inherit or create a console at startup, which is hosted separately by an instance of the console subsystem host process, conhost.exe. If you run a.exe from cmd.exe, it inherits the console of cmd.exe as its
StandardInput
,StandardOutput
, andStandardError
file handles -- while the single thread of "CMD itself" simply blocks until a.exe exits. -
Eryk Sun almost 7 yearsCMD's behavior to automatically search the working directory is insecure. You can fix it by setting the environment variable
NoDefaultCurrentDirectoryInExePath
. With this set, you will also have to use.\a
to run a.exe in CMD. But the working directory can still be added explicitly toPATH
as ".", which gives you more control over its position in the overall search. -
Bill_Stewart almost 7 yearsEveryone seems to have noticed that you did not post your error message. I believe their guesses are correct (PowerShell does not run executables in the current directory by default), but you should not make others guess. (Remember, nobody can see your screen.)
-
-
Bill_Stewart almost 7 yearsThe quotes around
.\a.exe
are not required since it does not contain any whitespace.