views:

122

answers:

5

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?

+1  A: 

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.

Mark Rushakoff
+1  A: 

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
}
GMan
+2  A: 

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.

jdc0589
-1 This is bad practice. Just use the shell properly.
Tyler McHenry
Wow...its a hello world app. There would never be a reason to use something like it in production code.
jdc0589
On the other hand, "Hello World" apps are written by beginning programmers who should be taught the correct way to do things, not the quick and dirty way.
Tyler McHenry
Or maybe if windows didn't make it extremely hard to develop C++ programs we wouldn't have to resort to such hacks. What about running the program from an IDE? It will open it, run it, and close it.
Falmarri
was going to mention the ide thing, but decided it wouldnt do any good. The only time I ever do this is when im in visual studio.
jdc0589
+8  A: 

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
Gnafoo
+2  A: 

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.

Tyler McHenry
Thanks for the warning, all these answers are great.
Mark