Learn to Write C++ Programs on Linux

0
28728
C++-Writing-Programs-on-Linux

So far, we have been writing and executing C++ programs using Turbo C++ and Dev-C++. Both IDEs run on Microsoft Windows machines. Now, it is time to switch a little to another operating system, and learn how to develop using C++ on Linux. Let’s get started.

Installing C++ Compiler on Linux Box
In your Linux (Red Hat/CentOS/Fedora) machine, type the following command as root to install the C++ compiler:

yum install -y gcc-c++*

To verify if the GCC compiler has been installed successfully

  • Use the rpm –qa command:

1

  • Use the which command:

2

Writing your First C++ Program on Linux
1. From your terminal, open a new file for editing using the vim command:

vim hello.cc

2. In the vim editor, type the following code:

#include <iostream>
using namespace std;
int main()
{
 cout << "Hello, this is my first C++ program on Linux" << endl;
 return 0;
}

3. Save and exit the file

4. To compile your new C++ program, type the following command from your terminal:

c++ hello.cc

If the compilation goes without errors, no output will be displayed.
3

5. An executable file is created in the current directory, with default name a.out.
4

6. To run the program, execute the generated executable the same way you execute any Linux executable.
5

Congratulations!

Specifying Name for the Resulting Executable
As I told you, compiling C++ programs without specifying options (as we did above) produces an executable file named a.out. If you want to specify a name of your choice for the resulting executable, you have two options: either to rename the default a.out after it is created, or to specify the executable filename on compilation using the –o option.

c++ hello.cc -o /opt/hello.run

Executing the named executable should produce the same output:
6

Executing System Commands from C++ Programs
It is necessary to be able to communicate with the system by executing operating system commands on need. The system() function allows you to run system commands from C++ code. For the compiler to recognize this function, and to compile successfully, the stdlib.h library file should be invoked.

Example
Write a program that displays the following system info:

  • Hostname.
  • IP address.
  • Date and time.
  • File systems utilization.

Type the following code in a new file (say system_commands.cc)

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
        system("echo -n '\nHostname: '"); system("hostname");
        system("echo -n '\nIP Address: '"); system("ip a");
        system("echo -n '\nDate/Time: '"); system("date");
        system("echo '\nFilesystem Utilization:'"); system("df -h; echo");
 return 0;
}

Compile the source file:
7

Now, execute it:
8

The getpid() and getppid() Functions
To get the Process ID (PID) of the current process, the getpid() is used.

Example
The following program prints the process ID of the program:

#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        pid_t PID;
        PID = getpid();
        cout << "The PID of the current process: " << PID << endl;
        return 0;
}

Compile and execute the program:
9

Notice the pid_t data type. It is a numeric data type that is used for process IDs.

The getppid() function returns the process ID of the parent process.

Example
Modify the previous program to print the PPID also.

#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        pid_t PID,PPID;
        PID = getpid();
        PPID = getppid();
        cout << "The PID of the current process: " << PID << endl;
        cout << "The PID of the parent process: " << PPID << endl;
        return 0;
}

When compiled and executed, this program should print both the process ID of the program, and its parent process ID.
10

The fork() Function
The fork() function is used to create a new (child) process by duplicating the calling (parent) process. The function returns the PID of the new child process (on success). On failure, -1 is returned instead. In the child process, 0 is returned on success.

Example
The following program starts a child process. If this operation is successful, the PIDs of both the current process and the child process are printed. If not, the program prints a message that it failed to start the child process.

#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        pid_t PID,CPID;
        PID = getpid();
        CPID = fork();
        if(CPID<0)
        {       cout << "Error starting child process\n";
                return 1;
        }
        else
        {
                cout << "The PID of the current process: " << PID << endl;
                cout << "The PID of the new child process: " << CPID << endl;
        }
        return 0;
}

Let’s see what we get when this program is run:
11

Notice that the child process is copy of the parent. That is why four lines got printed, instead of two. Notice also the difference between values of the child process ID: inside the parent, the PID of the child is returned by the fork() function and printed. On the other hand, the fork() function returns 0 to the child process.

Summary

  • To write C/C++ programs on UNIX/Linux machines, the GCC compiler is needed.
  • C++ programs are written and saved as .cc files.
  • The c++ and g++ commands compile and link C++ source files. If the compilation goes without errors, an executable file is created, with default name a.out.
  • The resulting executable can be executed the same way UNIX/Linux executables are executed.
  • The system() function is used to run system commands from C++ code.
  • The getpid() and getppid() functions return the process ID and the parent process ID of the program.
  • The fork() function provides a way for a process to run another program (process).

In the next article, we are going to talk about Input/Output and File Handling. See you.

Read More:

Previous articleLearn How to write a parser in Java
Next articleLearn Working with the Filesystem in Red Hat Linux

LEAVE A REPLY

Please enter your comment!
Please enter your name here