简体   繁体   中英

C++/G++ hello world application issue, maybe compilation

I am trying to compile my first c++ file on windows with the g++ compiler...

My cpp file is the following -

#include <iostream>
using namespace std;

int main ()
{
  cout << "Hello World!";
  return 0;
}

I type in this on command prompt to get to the directory

cd C:\Users\Mark

Then to compile my program I do

g++ hello.cpp

It creates a file name a.exe (default) but when I click on it the command prompt quickly flashes open then it's gone.

What did I do wrong? It should say Hello World! on the prompt and stay there, right?

What you did looks correct, but if you start the program by double clicking it in Windows, the new command prompt will be closed once it is finished. Since you already have a command prompt open for compilation, try to start the program from there, too:

g++ hello.cpp -o hello.exe
hello.exe

That's just normal Windows behavior of executing a command-line file from the UI -- as soon as the program exits (immediately in your case), the prompt window closes.

Running it from the command line will keep it up. To do that, Start > Run > cmd , then cd to the directory, then type a.exe .

Alternatively, you can wait for input from the user to keep the window open. That's fine for testing, but nobody who actually executes programs from the command line would want to have to hit a key to stop a program from running, when it should exit on its own correctly.

you didnt do anything wrong. There isnt a readline or anything at the end of your program, so when it is finishes executing it closes. Add a read character at the end of the program to make it wait for input to terminate.

You did everything right. Whether or not the command prompt stays open has nothing to do with your program. The command prompt is itself a program, and its default behavior is that when it launches in response to the execution of a console application, it closes immediately when the program is finished.

What you should do is launch the command prompt yourself and then run the program from within it, rather than launching the program by double-clicking its icon.

Do not fall into the bad practice of adding a call to some sort of pause function, or waiting for input at the end of the program. You should not get in the habit of hard-coding work-arounds for unwanted behavior of a particular shell into your application. Just use the shell the right way to get the behavior that you want.

The prompt has nothing to do with your program or the language. It has to do with your OS.

Your program should be run from the command line, which will (obviously) leave the command window up when it's finished.

There are tricks to make it stay up if you really want, but these are just tricks, and not necessarily good practice. One would be:

int main()
{
    std::cin.get(); // waits for enter
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM