Using Ubuntu, How Do You Install a C++ Compiler and Run a Basic C++ Program?

Problem scenario
You want to install a C++ compiler in Ubuntu.  You want to write a basic C++ program and run it.  How do you do these things?

Solution
1. As root, install a C++ compiler with this command:

apt install g++

2. Create a basic program.  Here is an example.  Name this file hello.cpp and put this as the content:

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello from ContinualIntegration.com!" << endl;
    return 0;
}

3.  Compile the code with this command:

g++ hello.cpp

4.  Run the byte code with this command:

./a.out

Leave a comment

Your email address will not be published. Required fields are marked *