cpp Command in Linux

Introduction

This lab will guide you through the process of compiling and running C++ programs within a Linux environment, leveraging the power of the g++ compiler. You'll begin by crafting a basic C++ program, subsequently employing the g++ compiler to generate an executable, and ultimately, running the program. Furthermore, you'll delve into more sophisticated C++ development methodologies, including compiling multiple source files, utilizing compiler flags to optimize your code, and effectively organizing C++ project structures. This lab imparts crucial skills for any systemadmin engaged in C++ development on the Linux platform.

This lab offers clear, step-by-step instructions on leveraging the g++ compiler for efficient C++ development, including managing C++ code within the Linux file system. This hands-on experience will equip you with the necessary skills to effectively compile and execute C++ programs in a Linux environment.

Compile and Run C++ Programs in Linux

In this section, you'll discover how to compile and run C++ programs in a Linux environment. We'll start by building a simple C++ program, followed by using the g++ compiler to generate the executable, and finally, executing the program.

First, let's create a new C++ file named hello.cpp in the ~/project directory:

cd ~/project
nano hello.cpp

Within the nano editor, insert the following C++ code:

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

Save the file and exit the nano editor.

Next, we'll utilize the g++ compiler to build the executable from the hello.cpp file:

g++ -o hello hello.cpp

This command compiles the hello.cpp file and creates an executable named hello.

Example output:

Now, let's run the compiled program:

./hello

This will execute the hello program and display the message "Hello, World!".

Example output:

Hello, World!

Well done! You've successfully compiled and run a C++ program in a Linux environment.

Utilize the g++ Compiler for C++ Development

In this part, you'll learn how to utilize the g++ compiler for more advanced C++ development tasks. This includes compiling multiple source files, using compiler flags for optimization, and managing complex C++ project structures.

Let's begin by creating a simple C++ project with multiple source files. First, create a new directory for the project:

cd ~/project
mkdir cpp-project
cd cpp-project

Now, create two C++ source files, main.cpp and utils.cpp, in the cpp-project directory:

nano main.cpp

In the main.cpp file, add the following code:

#include "utils.h"
int main() {
    printMessage();
    return 0;
}

Next, create the utils.cpp file:

nano utils.cpp

In the utils.cpp file, add the following code:

#include "utils.h"
void printMessage() {
    std::cout << "This is a utility function." << std::endl;
}

Finally, create the utils.h header file:

nano utils.h

In the utils.h file, add the following code:

#ifndef UTILS_H
#define UTILS_H
#include <iostream>
void printMessage();
#endif

Now, let's compile the project using the g++ compiler:

g++ -o app main.cpp utils.cpp

This command compiles the main.cpp and utils.cpp files and generates an executable named app.

Example output:

To run the compiled program, use the following command:

./app

This will execute the app program and display the message "This is a utility function."

Example output:

This is a utility function.

In this section, you learned how to:

  • Create a simple C++ project with multiple source files
  • Use the g++ compiler to compile the project
  • Run the compiled C++ program

Manage C++ Code with the Linux File System

Here, you will learn how to effectively manage your C++ code using the Linux file system. We'll cover essential file operations such as creating, moving, and deleting files and directories, which are fundamental for organizing and maintaining your C++ projects.

Let's start by creating a new directory for your C++ project:

cd ~/project
mkdir cpp-project
cd cpp-project

Now, let's create a new C++ file named main.cpp in the cpp-project directory:

nano main.cpp

In the main.cpp file, add the following code:

#include <iostream>

int main() {
    std::cout << "Hello, C++ on Linux!" << std::endl;
    return 0;
}

Save the file and exit the nano editor.

Next, let's compile the main.cpp file using the g++ compiler:

g++ -o app main.cpp

This command compiles the main.cpp file and generates an executable named app.

Example output:

Now, let's run the compiled program:

./app

This will execute the app program and display the message "Hello, C++ on Linux!".

Example output:

Hello, C++ on Linux!

To demonstrate file management, let's create a new directory for our project's source files:

mkdir src
mv main.cpp src/

This creates a new src directory and moves the main.cpp file into it.

Let's also create a new directory for our project's build artifacts:

mkdir build
mv app build/

This creates a new build directory and moves the app executable into it.

Finally, let's delete the build directory and its contents. Be cautious when using this command as root:

rm -rf build

This command removes the build directory and all its contents.

In this section, you learned how to:

  • Create and navigate directories in the Linux file system
  • Create, move, and delete C++ source files
  • Compile C++ code and manage the generated executable files
  • Organize your C++ project structure using directories

Summary

Throughout this lab, you've gained practical experience in compiling and running C++ programs within a Linux environment utilizing the g++ compiler. You successfully created a simple C++ program, compiled it into an executable, and ran the program. Furthermore, you explored more advanced C++ development techniques, including compiling projects with multiple source files, utilizing compiler flags for optimization, and strategically managing C++ project structures. These are fundamental skills for systemadmin and developers responsible for deploying and maintaining C++ applications on Linux systems.

400+ Linux Commands