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

Share:
19,159
Vrishank Gupta
Author by

Vrishank Gupta

Updated on June 04, 2022

Comments

  • Vrishank Gupta
    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 command a.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 as a.exe. Am I doing it the wrong way?

    • user4581301
      user4581301 almost 7 years
      To be honest I've been wimping out. I run cmd in powershell, then do the old cmd stuff the cmd way.
    • BLUEPIXY
      BLUEPIXY almost 7 years
      Try like this ./a
    • Eryk Sun
      Eryk Sun almost 7 years
      Running 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, and StandardError file handles -- while the single thread of "CMD itself" simply blocks until a.exe exits.
    • Eryk Sun
      Eryk Sun almost 7 years
      CMD'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 to PATH as ".", which gives you more control over its position in the overall search.
    • Bill_Stewart
      Bill_Stewart almost 7 years
      Everyone 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
    Bill_Stewart almost 7 years
    The quotes around .\a.exe are not required since it does not contain any whitespace.