Sunday, May 31, 2015

Beginning c/c++ compiling.

How can I compile a simple C or C++ program on Linux operating systems using bash Terminal application?

Tutorial details

Difficulty: easy
Root privileges No
Requirements GNU C/C++ compiler
Estimated completion time 10m

GNU C and C++ compiler collection
Development tools
Development libraries
IDE or text editor to write programs


Part 1: Install C/C++ compiler and related tools

To compile a C or C++ program on any Linux distro such as Ubuntu, Red Hat, Fedora, Debian and other Linux distro you need to install:

>Fedora, Redhat, Centos, Or Scientific linux:


# yum groupinstall 'Development Tools'

>Debian, Ubuntu, or Mint

$ sudo apt-get update
$ sudo apt-get install build-essential manpages-dev

To verify the install, type the following commands to display the version number and location of the compiler on Linux:

$ whereis gcc
$ which gcc
$ gcc --version

Sample outputs:

$ whereis gcc
gcc: /usr/bin/gcc /usr/lib/gcc /usr/bin/X11/gcc

$ whereis g++
g++: /usr/bin/g++ /usr/bin/X11/g++

$ gcc --version
gcc (Debian 4.7.2-5) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gcc
gcc: fatal error: no input files
compilation terminated.
$


Part 2:  GNU C compiler on Linux. Create a file called demo.c using a text editor such as vim or nano
 
#include<stdio.h>
/* demo.c: My first C program on a Linux
 */
int main(void) {
printf("Hello! This is a test program.\n");
return 0;
}

Save as demo.c

How do I compile the program on Linux?

Use any one of the following syntax to compile the program called demo.c:

$ cc program-source-code.c -o executable-file-name

OR

$ gcc program-source-code.c -o executable-file-name

In this example, compile demo.c, enter:
$ cc demo.c -o demo

If there is no error in your code or C program then the compiler will successfully create an executable file called demo in the current directory, otherwise you need fix the code. To verify this, type:

$ ls -al demo*
-rwxr-xr-x 1 eddie eddie 4884 May 31 20:35 demo
-rw-r--r-- 1 eddie eddie  149 May 31 20:35 demo.c

How do I run or execute the program called demo on Linux? Just simply type the the program name

 $ ./demo

OR

$ /path/to/demo

 $ ./demo
Hello! This is a test program.


Part 3: GNU C++ compiler on Linux

Create a program called demo.cpp as follows:

#include "iostream"
// demo2.C - Sample C++ program
int main(void) {
std::cout << "Hello! This is a C++ program.\n";
return 0;
}

To compile this program, enter:

$ g++ demo.cpp -o demo


To run this program, type: ./demo

$ ./demo
Hello! This is a C++ program.


Part 4: More information.
How do I generate optimized code on a Linux machine?

The syntax is as follows C compiler:
cc -O input.c -o executable
The syntax is as follows C++ compiler:
g++ -O -Wall input.C -o executable
How do I compile a C program that uses math functions?

The syntax is as follows when need pass the -lm option with gcc to link with the math libraries:
cc myth1.c -o executable -lm
How do I compile a C++ program that uses Xlib graphics functions?

The syntax is as follows when need pass the -lX11 option with gcc to link with the Xlib libraries:
g++ fireworks.C -o executable -lX11
How do I compile a program with multiple source files?

The syntax is as follows if the source code is in several files (such as light.c, sky.c, fireworks.c):
cc light.c sky.c fireworks.c -o executable
C++ syntax is as follows if the source code is in several files:
g++ ac.C bc.C file3.C -o my-program-name

See gcc(1) : Linux and Unix man page for more information.

$ man gcc

No comments:

Post a Comment