Post

Running a C/C++ programme on Linux

compiling & running c/cpp programmes

install GNU C/C++ compiler
1
2
sudo apt-get update
sudo apt-get install build-essential manpages-dev
verify installation
1
2
3
whereis gcc
which gcc
gcc --version
1
2
3
whereis g++
which g++
g++ --version

compiling C program

  • helloWorld.c
1
2
3
4
5
6
7
8
#include<stdio.h>

int main(void)
{
 printf("Hello World !.\n");
 printf("This is 'C'. \n");
 return 0;
}
  • compiling C program
1
cc helloWorld.c -o helloWorld

or make helloWorld or gcc helloWorld.c -o helloWorld

  • running a C program
1
./helloWorld

compile & running cpp program on Linux

  • helloWorld.cpp
1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main ()
{
  cout << "Hello World! ";
  cout << "\n";
  cout << "I'm a C++ program";
  cout << "\n";
  return 0;
}
  • compile
1
g++ -o helloWorld helloWorld.cpp

or

1
make helloWorld
  • run
1
./helloWorld

generating symbolic information for gdb and warning messages

1
cc -g -Wall helloWorld.c -o helloWorld
1
g++ -g -Wall helloWorld.c -o helloWorld

optimized code

1
cc -O helloWorld.c -o helloWorld
1
g++ -O helloWorld.c -o helloWorld

compiling a C program that uses math functions/Xlib graphics functions

link the library using -l option

1
cc myth1.c -o executable -lm

compile a program with multiple source files

1
cc light.c sky.c fireworks.c -o executable
1
g++ ac.c bc.c file3.c -o my-program-name
This post is licensed under CC BY 4.0 by the author.