Notes

Running and Compiling C/C++ code on Linux

Edit on GitHub

Programming

C/C++ support is usually already there on Ubuntu

1g++ --version
2gcc --version
  • If not, install build-essential
  • build-essential includes gcc, g++ and make as well as libc-dev
1sudo apt install build-essential manpages-dev

The file extension is .cpp on Linux or .C

Sample program

hello.cpp
1#include <stdio.h>
2
3int main() {
4  printf("bonjour le monde!");
5  return 0;
6}

Compile code

g++ hello.cpp

By default it’ll generate a file called a.out

1g++ hello.cpp -o output

output will be the generated binary file

Run code

1./output