Linux is the most popular operating system for development of the software. Because it is the open source operating system and it has many open source applications, compiler, debuggers, IDE, and tools. Linux has different free and special compilers for C and C++ programming language. This operating system also has special tools for developing Java programming languages such as Netbeans, KDevelop, codelite, and Eclipse IDE. You can hire freelancers who have knowledge and experience of working with Linux operating system.
The C++ is the object-oriented and high-level programming language. It is the simple language, and it provides a structured approach, rich set of library functions, and data types. The compilation and execution of C++ programming language are fast. The C++ is the high-level programming language compared to assembly language programming and low-level language in comparison to Java programming language. Many of the operating systems are in C and C++ language. Because C++ is the powerful programming language with a wide variety of function, data types, control statement, and decision making statements. C++ is the compiler based programming language it means that without compilation no C++ program can be executed. For the execution of program first, you have to compile your program and then execute it. It is the article which gives a complete idea to the programmer regarding how to compile and run the C++ program on Linux operating system.
Installation of C/C++ Compiler
For installation of C++ compiler that is GNU on Linux, Fedora, CentOS, and Red hat you have to know about the yum command
Linux: #yum groupinstall ‘Development Tools’
Suppose you are using the Debian and Ubuntu Linux then you will need the following command.
$ sudo apt-get install build-essential manpages-dev
For knowing the installation of GNU, version number, location of the compiler use following commands.
Create Folders for Saving Your Program:
To make the directory on desktop use the following commands. You can find freelance jobs online in this sector easily.
sudo mkdir -p CPP/ Programs
Use above command, CPP folder gets created and Programs as subfolders inside the main folders.
Changing of Working Path:
For changing the working path of your terminal, you must use the following command.
Creation of Simple Program:
You can create the simple program using any simple text editor such as vi, emacs or joe and nano. Here you will find the nano text editor for creation of a program. The nano text editor is the simple text editor to create and write a program. To make program file in nano use the following command.
Sudo nano helloworld.c
Now write the helloworld program in nano editor
Sample C++ prgoram
std::cout << “\nHello World! This is a C++ program in Linux”;
For saving the program press Cntrl+X on the keyboard and at the same time press Y and press Enter.
Compilation of C++ Program:
For a compilation of your program type the following command.
gcc -Wall -W -Werror helloworld.c -o helloworld
This command executes the GNU C compiler to compile the file helloworld.c and output is given to an executable called helloworld.
The word –Wall –W allows the compiler to check the warnings.
If you want to compile the program then use the following command
g++ -Wall –W –Werror helloworld.cpp –o helloworld
If your C++ program has no error, then your executable file is successfully created by a compiler in the current directory otherwise the code needs to be debugged.
Execution of C++ Program:
For the execution of helloworld program use the following command. You can find freelance jobs online in this sector easily.
After typing this command, the program output displays on the Terminal. In place of name helloworld, you can use your file name of your program.
Optimisation of code on Linux Machine:
If you want to optimise your code then type the following command
Following is the command for C programming language.
Cc -O input.c -o executable
Following is the Command for C++ programming language.
g++ -O -Wall input.C -o executable
Compilation of Program with Multiple Source Files:
If your source code is available in multiple files then use the following Command
g++ ac.C bc.C file3.C -o my-program-nam
This article gives a complete idea to programmer regarding how to compile and run the C++ program on Linux operating system. As Linux is an open source operating system, so it has a lot of free debuggers, compiler, IDE, and tools. Because of these available free tools creation of a program in Linux operating system for programmer becomes easy and simple.