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
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;
}
|
1
| cc helloWorld.c -o helloWorld
|
or make helloWorld
or gcc helloWorld.c -o helloWorld
compile & running cpp program on Linux
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;
}
|
1
| g++ -o helloWorld helloWorld.cpp
|
or
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
|