Learn to Write C++ Programs on Linux

0
4381
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:

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:

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

3. Save and exit the file

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

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.

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)

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:

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.

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.

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here