What is the command to run the following simple C++ program?

#include <iostream>
using namespace std;

// main () is where program execution begins
int main()
  cout << "Hello World";        // prints Hello World

  return 0;

6 Answers 6


First you need to compile and link your program. Assuming the source code is saved in a file yourprog.cpp, you can do that using the GNU C++ compiler g++, for example

g++ -Wall -o yourprog yourprog.cpp

The resulting executable will be called yourprog and you can then run it using

  • sudo apt-get install build-essentials is probably needed, too. ;-)
    – Rmano
    Nov 23, 2014 at 20:12
  • 2
    Not really - since the OP isn't using make, and the dependency tree for g++ should pull in the standard libs. Also, it's called build-essential (singular). Sep 24, 2016 at 19:02

Here's a way to use make to build and run your program without requiring any additional setup:

make CXXFLAGS='-Wall -Werror' hello_world && ./hello_world

But assuming you will continue developing, you will want to create a file called Makefile to streamline things further.

CXXFLAGS = -Wall -Werror
default: build
build: hello_world
run: build

Then you can build and run your program by typing:

make run

If you just want to see if your program compiles without error, type:


Other notes:

  • The <tab> above should be created using the tab key.
  • It is important to include -Wall -Werror. These flags prevent certain obvious programming bugs from being ignored by the compiler. That means less debugging work for the programmer.
  • I advocate the use of the -s option with make. It eliminates (usually) unnecessary verbosity.
  • One feature of make is that it doesn't recompile your program if it doesn't need to. This can be a nice time-saver if the program takes a long time to compile. This is especially useful if your project has more than one source (.cpp) file, since these can be compiled independently -- and even in parallel (simultaneously) with the -j option.

open a terminal and execute the following

1- g++ -o outfilename.bin source.cpp

2- ./outfilename.bin

that assumes the source file is source.cpp


There's a nifty package called binfmtc that allows you to run C++ files as if if they were "scripts" -- not requiring an explicit compilation step.

Once the binfmtc package is installed, your source file needs a /*BINFMTCXX: ... header comment (see example) and mark the .cpp file as executable using chmod +x.

/*BINFMTCXX: -Wall -Werror
#include <iostream>
using namespace std;

// main () is where program execution begins
int main()
  cout << "Hello World";        // prints Hello World

  return 0;

Setup steps:

$ sudo apt install binfmtc
$ chmod +x ./first-time-to-run-c-program-on-ubuntu-14-04.cpp

Running the "script":

$ ./first-time-to-run-c-program-on-ubuntu-14-04.cpp
Hello World
  • Open terminal by pressing Ctrl+Alt+t

  • Install GNU C++ Compiler by running following command:

    sudo apt-get install g++  
  • Now compile the cpp source file by running this command:

    g++ sourceFile.cpp -o anything  
  • You can now run the file like this:


First, you save it to a file, most likely by pressing ctrl+s, then compile it. To compile it, run the following command:

g++ path/to/that/file.cpp -o path/to/output/file.out

Then just drag the output file to the terminal window to run it, because it is now an executable file.

You need to use g++ because c++ is a compiled language, meaning you need a compiler to compile it into machine code. This results in c++ being a faster language than others.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .