How to Set Up C++ in Linux
Linux is a great C++ coding
environment. Most Linux distros already have the essential components
preinstalled, such as a compiler and the text editors are excellent for entering code into,
including colour coding; there’s also tons of extra software available to help you out.
LINUX++
We’re going to be using a fresh
installation of Linux Mint for this particular tutorial. More on Linux Mint can
be found in the next section of the book.
Step 1
The first step with ensuring
Linux is ready for your C++ code is check the system and
software are
up to date. Open a Terminal and
enter: sudo apt-get update && sudo apt-get upgrade.
Press Return and enter your password. These commands updates
the entire system and any installed software.
Install C++ in Linux |
Step 2
Most Linux distros come
preinstalled with all the necessary components to start
coding in C++.
However, it’s always worth
checking to see if everything is present, so still within the Terminal,
enter: sudo apt-get install build-essential and press Return.
If you have the right components, nothing is installed
but if you’re missing some then they are installed by the command.
Install C++ in Linux |
Step 3
Amazingly, that’s it. Everything
is all ready for you to start coding. Here’s how to
get your first C++
program up and running. In Linux
Mint the main text editor is Xed can be launched by clicking on
the Menu and typing Xed into the search bar. Click on the Text
Editor button in the right-hand pane to
open Xed.
Install C++ in Linux |
Step 4
In Xed, or any other text editor
you may be using, enter the lines of code that make
up your C++ Hello World program. To remind you,
its:
- #include <iostream>
- int main()
- {
- //My first C++ program
- std::cout << “Hello World!\n”;
- }
Install C++ in Linux |
Step 5
When you’ve entered your code,
click File > Save As and choose a folder where you
want to save your program. Name the file as
helloworld.cpp, or any other name just as long as it has .cpp as the extension. Click Save to
continue.
Install C++ in Linux |
Step 6
The first thing you can see is
that Xed has automatically recognised this as
a C++ file, since the
file extension is now set to
.cpp. The colour coding is present in the code and if you open up the file
manager you can also see that the file’s icon has C++ stamped on it.
Install C++ in Linux |
Step 7
With your code now saved, drop
into the Terminal again. You need to navigate to
the location of the C++ file you’ve just saved.
Our example is in the Documents folder, so we can navigate to it
by entering: cd Documents. Remember, the Linux Terminal is
case sensitive, so any capitals must
be entered correctly.
Install C++ in Linux |
Step 8
Before you can execute the C++
file you need to compile it. In Linux it’s common
to use g++, an open source C++ compiler and as you’re
now in the same folder as the C++ file, go to the Terminal,
enter: g++ helloworld.cpp and press return.
Install C++ in Linux |
Step 9
There will be a brief pause as
the code is compiled by g++ and providing there are no
mistakes or
errors in the code you are
returned to the command prompt. The compiling of the code has created
a new file. If you enter ls into the Terminal you can see that alongside your C++ file is
a.out.
Install C++ in Linux |
Step 10
The a.out file is the compiled
C++ code. To run the code enter: ./a.out and press
Return. The words ‘Hello World!’ appears on the
screen. However, a.out isn’t very friendly.
To name it something else
post-compiling, you can recompile with:
g++ helloworld.cpp -o helloworld.
This creates an output
file called helloworld which can be run with:
./helloworld.
Install C++ in Linux |
0 Response to "How to Set Up C++ in Linux"
Post a Comment