Something about programming

How to compile C++ code with g++

Next tutorial: Hello World in C++

Before we start you need to have g++ installed on your operating system. g++ is derived from the name gcc. gcc is a compiler for C language. gcc stands for GNU C Compiler. GNU is operating system and also it's a set of free software, including gcc and g++ - compilers for C/C++.

If you are working on Linux there is g++ already on your computer.

g++ for Windows

There is couple options to get g++ on Windows. Cygwin and MinGW32. MinGW also has version for x86-64 architecture. And that's one we'll use. Download MinGW-W64, install it and then find mingw in start menu (MinGW-W64 -> Run Terminal) or find where you installed it in explorer (there is file mingw-w64.bat that you need to run). You'll see the Windows command line.

Working with Windows command line

If you never worked with command line, here is couple commands that you'll need:

d: c: cd folder1 cd ..

Name of the disc with colon - go to specific disk. For example, I store all my code on disk D so first command for me will look like:

d:

cd - change directory. This command will move you to specific directory. I store my code for this tutorials in folder: d:\prog\cpp. So after I've choosen disk D, I usually enter this command:

cd prog\cpp

cd .. (two dots) will move you to the folder above.

Source code

Create file hello_world.cpp in the folder where you plan to store your code. Open it with plain text editor: Notepad/Gedit/Notepad++/Sublime... Paste this text:

#include <iostream> int main() { cout << "Hello World\n"; return 0; }

This program prints line Hello World in console. We'll discuss what this code means in next tutorial.

As you can see we store code in plain text files. This is source code. To make it actually work we need to compile it. Now open the folder where you saved the file with source code in command line.

Compile C++ code with g++

g++ <filename>
g++ hello_world.cpp

New file where created in current directory: a.out or a.exe. To run the app just print the file name in the console and hit Enter. You'll see text Hello World in console. After that your program will immediately stop.

a.out is the default output name of the compiled file. We can change it:

g++ -o <output_filename> <filename> g++ -o hello_world.exe hello_world.cpp

By running last line I'll receive the file hello_world.exe as output.

Then on Windows you can run your compiled program with command:

hellow_world.exe

And on Linux:

./a.out

Compilation and Linking Phases

When we run compiler there are actually two steps. First, compiling of source code files. The result of this step is so called object code. New file with extension .o is created. The next step is linking - special program linker takes object file/files and "link" them together. Linker is necessary when there are more than one source code file. We'll return to linker later. For now all our programs will consist only from one file.

Conclusion

We discussed how to compile C++ source code with g++ compiler. Now we are ready to start learning C++. And we'll start with discussing Hello World.

Exercises

  1. Compile hello_world.cpp

Comments:

No comments yet