TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

C++

Run C++ Program on Linux

Overview

In this tutorial, we will learn the steps involved to run C++ program on a Linux machine. This is a step-by-step guide with screenshots and code.

Environment

  • Linux machine
  • C++ compiler
  • vi Text Editor

Install GNU C++

In this step, we need to install the GNU C++ compiler.

https://www.testingdocs.com/install-gcc-c-compiler-on-opensuse/

Create C++ Source File

Open Terminal/ Konsole.

Change the working directory path to a suitable directory.

Create a file with .cpp file extension. For example, we will create a hello.cpp file.

$ touch hello.cpp

Create C++ File Command Line Linux

Edit the C++ source file

Type the following command

$ vi hello.cpp

Enter insert mode. Enter the “i” keyboard key.

Write C++ Program

Enter the C++ code. For simplicity, we will write a simple C++ program to print Hello, World! message to the computer screen.

C++ Program vi Text Editor

Save the C++ file

Once done with the C++ code. Enter the esc key. Type wq and hit the Enter key to save the file and exit the vi text editor.

Compile C++ Program

To compile the C++ program type the following command:

$ g++ hello.cpp

This will create the a.out object file in the working directory.

C++ Source File and Object Code Linux

Run C++ Program

To run the C++ program type the following command:

$ ./a.out

Run C++ Program Command Line Linux

We should be able to see the message “Hello, World!” displayed on the computer screen.

a.out is the default executable output file. We can override this with the -o flag during the compilation process. To name the executable file:

$ g++ hello.cpp -o hello

$ ./hello

Compile C Plus Program Name

That’s it. We have successfully created and run a sample C++ program on the Linux machine.

—

C++ Tutorials

C++ Tutorials on this website:

https://www.testingdocs.com/c-coding-tutorials/

For more information on the current ISO C++ standard

https://isocpp.org/std/the-standard

Related Posts

g not found OpenSuse

C++ /

Install GCC C++ Compiler on OpenSuse

sum of digits c program

C++ /

Sum of Digits of a Number C++ Program

C++ Hello World Program

C++ /

C++ Hello World Program

C++ /

Object-Oriented Programming Language Examples

C++ /

Object-Oriented Programming Features

‹ Define a C++ Class in Code::Blocks IDE› Define C++ Class Constructor

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version