C++ - Windows - Hello World!

Let's create our first C++ application.

For this tutorial, we will use:

  • Eclipse Indigo CDT
  • Cygwin

If you install your Cygwin binary into a different drive than from your development directory you can still use it.
For example if you have installed Cygwin in C:/soft/ and want to work in D:/dev/, go until C:/soft/ and run your Cygwin.bat file.
Once open, write it:

cd /cygdrive/

then press the TAB key on your keyboard.
You have now access to all your drives.

The Makefile:

##Variables
NAME    = gogogo.exe
SRC     = src/main.c
OBJ     = $(SRC:.c=.o)

##Rules
$(NAME) : $(OBJ)
    g++ $(OBJ) -o $(NAME)
all     : $(NAME)
clean   :
    rm -f $(OBJ)
fclean  : clean
    rm -f $(NAME)
re        : fclean all

The next file will be placed in a directory named src.
This is the main.cpp source file that will contain cout and cin.

  • cout is for display something to the user.
  • cin is for enter something with the keyboard to be captured by a variable, for example.

Note that for the cout, we will use << and for cin >>.
Let's see this file:

#include <iostream>

using namespace std;

int    main() {
    int myVar;

    cout << "Hello!" << endl;
    cin >> myVar;
    cout << "You tried to enter " << myVar;
    return 0;
}

Go until your directory where there is your project and open a command line with Cygwin and enter this:

make

It will compile all your .cpp file into .o ones.
Then all .o will be linked into the target file, in our case gogogo.exe.

You can also enter:

make re

Why "re"?

Because in the Makefile there is a rule saying that the re command will activate fclean then all.

The result is a new file in your directory named gogogo.exe.

Double click it and you have a black window asking you to enter a number before it disappears.

It was maybe your first application in C++ on Windows, so good job! cool

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.