Gentoo Wiki



Starting with C++

g++ (GNU C++ compiler)

First of all, you need g++, which is most likely already installed if you use Gentoo. It's best to make sure, though:

Code: Checking g++ version
g++ --version

At my box here it returns:

Code: Output
 g++ (GCC) 3.3.5  (Gentoo Linux 3.3.5-r1, ssp-3.3.2-3, pie-

Hello World!

Right, on to the real thing! Fire up your editor and insert this into a new file:

File: helloWorld.cpp
#include <iostream>

int main () 
       std::cout << "Hello world in ANSI-C++\n";
       return 0;

Save the file as helloWorld.cpp. Now we have to compile it to a binary file, so we can run it. Run the following commands:

Code: Compile and run your program.
 $ g++ helloWorld.cpp
 $ ./a.out

The latter executes your freshly compiled program.

Make Life Better

All right, a couple of things can be done better. First of all, let's make some changes to the source code:

File: A better helloWorld.cpp
#include <iostream>

using namespace std;

int main () 
       cout << "Hello world in ANSI-C++\n";
       return 0;

Now we won't have to type std:: each time.

A file named a.out isn't very nice either, instead of running

g++ helloWorld.cpp

We should run:

g++ helloWorld.cpp -o hello

The generated file will then be called hello instead of a.out.

Note that the compiler will write "return 0" for you, but only for the main function.


  1. Create a .cpp file
  2. Compile it: g++ helloWorld.cpp -o hello
  3. Alternatively compile it for Windows using i386-mingw32msvc-g++ ./helloWorld.cpp -o hello.exe
  4. Run it! ./hello

That's pretty easy, huh? Buy a fun book about C++ and learn the language!


Retrieved from ""

Last modified: Mon, 13 Oct 2008 03:33:00 +0000 Hits: 18,083