There is always a worldwide debate among developers about which operating system to choose to create work applications, but in recent times GNU/LINUX has become a major share of use in this segment because its flexibility, cost, security and compatibility with most programming languages have made it a solid alternative for work.

C++ are programming languages well known for the vast amount of programs created with them, in addition to their long history in the world of computers, which is why it serves as an entry language for all those who want to learn programming from the beginning.

GNU/LINUX and C++ is an ideal combination if you want to learn programming at a low cost

1.- Installing compiler and others libraries

If we want to write and compile a code, we must have installed the compiler of that language, additionally we can also install other libraries associated with the language to extend its functionality. Open a terminal and log in as root user and then:

For Debian and Ubuntu:

On Opensuse LEAP and Tumbleweed

For CentOS 7:

1.- installing build-essential
1.- installing build-essential

The above commands not only install the C++ compiler which is g++ but also other tools for building packages.

 

2.- Creating the .cpp files

We have large editors to create our text files, from integrated in the terminal (nano, vi…), complete IDE (netbeans, eclipse) or text editors (atom, komodo, visual code), depending on the taste of each one, but as we are going to compile from the terminal, we will use nano.

And in it we started to write our code for example:

Then we save and close and go to the console to compile the file.

2.- Coding on nano
2.- Coding on nano

3.- Compiling the code

Now we save and exit nano with ctr+x and invoke g+++, that is, the C++ compiler. Write:

We explain briefly, g+++ is the compiler; -or is a flag option that indicates that the next element is the name of the executable; executable is the name of the binary that is generated after compiling the file, you can put the name you want; helloworld.cpp is the name of the.cpp file that is going to be compiled, you can also indicate an absolute path of a file.

If the code is spelled correctly and without errors, when executing that command we should not have any output messages.

3.- using g++
3.- using g++

Product of the previous command an executable is generated.

4.- Run a c++ program from an executable in the terminal

To run an executable file created by g+++ on a terminal, type the following:

4.- Executing a code
4.- Executing a c++ code

In some cases we can find compatibility errors with g+++ when using many specific functions within our code, for this we will use:

and the problem should be solved.

Compile with g+++ a program that requires libraries

The above example we compiled a simple code, but what if we want to compile one that requires other libraries?

First we will create 3 files, the first one will be the main one, then we will make the library and the code of that library. To do this we will make a main code that simply multiplies two numbers, but using an external library.

In it we write:

5.- Creating the "main" cpp file
5.- Creating the “main” cpp file

Now we’ll do the.h file from the library

And inside the editor we place the following content

6.- Creating the second file
6.- Creating the second file

we finally created the last file:

And here’s what we put in:

7.- Creating the third file
7.- Creating the third file

Now we proceed with the compilation:

We proceed to explain: unlike the previous example, we add the -Wall option so that it shows us all the warnings that occur during the compilation; then we compile all the.cpp that are really the sources of the program; and with the instruction -o we point to the output file.

8.- Compiling several .cpp files
8.- Compiling several .cpp files

Now we run:

9.- Running the output file
9.- Running the output file

And that’s it. As we can see it is easy to compile files with g+++ and GNU/LINUX, we would need to learn all the options that the compiler gives us from the terminal, although for that we would need another article.

Please, spread this article through social networks.

Spread the love
  • 4
    Shares

PS. If you like this post please share it with your friends on the social networks using the buttons above.Thanks.

LEAVE A REPLY

Please enter your comment!
Please enter your name here