11 C
Texas
angeloma
Senior Writer and partner

How to Compile a C++ code on GNU/LINUX?

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:

- Advertisement -

For Debian and Ubuntu:

:~# apt install build-essential

On Opensuse LEAP and Tumbleweed

:~# zypper install -t pattern devel_C_C++

For CentOS 7:

:~#yum groupinstall 'Development Tools'

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.

:~$ nano helloworld.cpp

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

#include<iostream>
using namespace std;
     int main(){
        cout<<"Hello World";
        return 0;
      }

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:

:~$ g++ -o executable helloworld.cpp

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:

:~$ ./file_name

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:

:~$ g++ -std=c++11 -o executable helloworld.cpp

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.

:~$ nano main.cpp

In it we write:

#include<iostream>
#include"library1.h"
using namespace std;
int main(){
int result = multiplication(1,1);
cout << "result is: " << result << "\n";
return 0;

}

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

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

:~$ nano library1.h

And inside the editor we place the following content

int multiplication(int,int);

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

we finally created the last file:

:~$ nano library1.cpp

And here’s what we put in:

#include"biblioteca1.h"

int multiplication(int a, int b){
return a+b;

}

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

Now we proceed with the compilation:

:~$ g++ -Wall main.cpp libreary1.cpp -o executable2

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:

:~$ ./executable2

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.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article