Tuesday 22 November 2016

ABOUT GCC & HOW TO RUN C PROGRAM ON GCC


Original GNU C Compiler (GCC) is developed by Richard Stallman, the founder of the GNU
Project. Richard Stallman founded the GNU project in 1984 to create a complete Unix-like operating system as free software, to promote freedom and cooperation among computer users and programmers.

GCC is a key component of "GNU Toolchain", for developing applications, as well as operating systems. The GNU Toolchain includes:
Ø  GNU Compiler Collection (GCC): a compiler suit that supports many languages, such as C/C++, Objective-C and Java.
Ø  GNU Make: an automation tool for compiling and building applications.
Ø  GNU Binutils: a suit of binary utility tools, including linker and assembler.
Ø  GNU Debugger (GDB).
Ø  GNU Autotools: A build system including Autoconf, Autoheader, Automake and Libtool.

Ø  GNU Bison: a parser generator (similar to lex and yacc).

Versions:
With Ubuntu 10.04 Linux
$ gcc –version
Gcc (Ubuntu 4.43-4ubuntu5.1) 4.43
$gcc –v
Help
You can get the help manual
$gcc –help



Man Pages
You can read the GCC manual pages
$man gcc

Getting started with Programming
The GNU C compiler is gcc
Compile/Link a simple C Program hello.c
Here is Hello-World C program hello.c
//hello.c
#include<stdio.h>
 int main(){
Printf(“Hello world!\n”);
Return 0;
}
To compile hello.c
$ gcc hello.c
The default output is called “a.out”
To run the program
$./a.out
Note:
·         In Bash or Bourne shell, the default PATH does not include the current working directory. Hence, you may need to include the current path (./)
·         You may need to include the file extension, i.e., "./a.out".

To specify the output filename, use -o option:
 Gcc –o hello hello.c
//Compile and link source file hello.c into executable hello
$ ./hello
//Execute hello, specifying the current path ./

Compile and link separately
//Compile only with –c option
$ gcc –c hello.c
//link the object files into and executable
Gcc –o hello hello.o
The options
Ø  -c: compile into object file "Hello.o". By default, the object file has the same name as the source file with extension of ".o" (there is no need to specify -o option). No linking with other object file or library.
Ø  Linking is performed when the input file are object files ".o" (instead of source file ".c"). GCC uses a separate linker program (calledld.exe) to perform the linking.

Compile and Link Multiple Source Files
Suppose that your program has two source files: file1.c, file2.c. You could compile all of them in a single command:

$ gcc –o prog file1.c file2.c
However, we usually compile each of the source files separately into object file, and link them together in the later stage. In this case, changes in one file does not require re-compilation of the other files.
$gcc –c file1.c
$gcc –c file2.c
$gcc –o prog file1.o file2.o

$./prog

Happy Coding!!! 

No comments:

Post a Comment