INSTALLING/USING G++ (Unix/Linux)



Most Unix/Linux users are already familiar with how to install packages on their system, and some Unix/Linux distributions will install C++ by default. However, if you need to install C++, the package name is usually referred to as "g++". Since different Unix/Linux distributions use different package managers, the easiest thing to do is to just try to type the g++ command on the command line, like this:

ShellPrompt% g++

If g++ is installed you may see an error messages that looks like this:

g++: error: no input files

If it is NOT installed, you will likely see a message telling you how to install it.

Once g++ is installed, you can easily compile a C++ program (we'll assume there's a program in a file named "main.cpp") with the following command:


ShellPrompt%. g++ -o main main.cpp


Assuming the compilation runs successfully, it will create an executable named "main". You would then run your program with the following command:


ShellPrompt% ./main


And that's all there is to using G++ on Unix/Linux!