3

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?

5
  • 2
    To be honest I've been wimping out. I run cmd in powershell, then do the old cmd stuff the cmd way. Commented Jul 25, 2017 at 3:15
  • 4
    Try like this ./a Commented Jul 25, 2017 at 3:16
  • 1
    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. Commented Jul 25, 2017 at 3:44
  • 1
    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. Commented Jul 25, 2017 at 3:48
  • 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.) Commented Jul 25, 2017 at 19:34

3 Answers 3

2

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"
Sign up to request clarification or add additional context in comments.

1 Comment

The quotes around .\a.exe are not required since it does not contain any whitespace.
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

Comments

1

[Working 100%] from a Windows user. Open the terminal(PowerShell) where your file.cpp is created.

  1. g++ file.cpp //it will compile the file into a.exe
  2. .\a.exe //this will run the program in windows.
  3. .\a.out //this will run the program in linux.

1 Comment

Number 3 will potentially give an error on most linux shells along the lines of .a.exe does not exist or more dangerously run the file .a.exe which would be a hidden file in the working directory. You likely meant that ./a.exe will work on most linux shells. While, in fact, it also works fine in powershell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.