How to edit, compile, and run C++ programs

Edit me

Edit

Open up your favorite text editor, (gedit, emacs, or vi, refer to this quick manual question 1), create a file with .cpp extension. For example, I could create a simple c++ program like the following:

#include <iostream.h>
int main() {
  cout << "Hello, world." << endl;
  return 0;
}

Saved with a name: testing.cpp

Compile

The C++ compiler is usually named c++ or g++, at a command line, type c++/g++ -o executable_file source_file. For example, you type:

g++ -o testing testing.cpp

or

c++ -o testing testing.cpp

testing is the out come executable file name and testing.cpp is the source file to be compiled

Run

  • If you have your current working directory in your path, then simply type:
    testing
    
  • If you are not sure, type:
    ./testing
    

    Instead