Full Version: Intro to the GNU C Compiler
Dream.In.Code > Programming Tutorials > Linux Tutorials
Nova Dragoon
Comming from a VC6 background in my first programming class, I found it somewhat difficult to start programming when I made the switch to Linux.
Here, I am going to go over some of the basic things you'll need to start programming in a unix environment with GCC.



The first big difference from Visual C, is that, there is no shiny IDE that does everything for you.

GCC runs from the command line and sometimes requires you to feed it big hairy strings in order for you to compile correctly.


First we will start off with a simple hello world program.
CODE

#include <stdio.h>

int main()
{
printf("Hello World\n");
return 0;
}


This can be written in any plain text editor you wish. Examples are VIM, Emacs, Pico, gedit, kedit, etc.

Save as "hello.c" to a directory you'll be working in.

Open a terminal and change directory to where you saved your file.


to compile:

gcc -o hello hello.c

the command gcc is the gnu c compiler. The -o flag is stating the name of the execuable file, hello.c is our source code file.

To execute:
./hello

Most of the time, thats all you will have to do, simple!


Some times you will want to include code from dynamic libraries that gcc doesn't do automatically, one example is the math library.


CODE

#include <stdio.h>
#include <math.h>

int main()
{
float a;
a=sqrt(25.0);

printf("%f\n",a);
return 0;
}


Save as square.c

When you run gcc -o square square.c
you'll get back an ugly error such as:
QUOTE

/ccKQL5eR.o(.text+0x20): In function `main':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status


That is because you are not linking with the math library.
The math library lives in /usr/lib/ as libm.a and libm.so (static and dynamic libs)

To compile with the math library add the -lm flag to gcc
gcc -lm -o square square.c

To use other libraries:
find thier name (ex. libGL.so)
The flag that should be added is -lGL
Tom9729
Nice tutorial. icon_up.gif

One thing worth noting for the new user. There are quite a few IDE's out there that use GCC as a compiler.

Just a few off the top of my head: Eclipse, KDevelop, Anjuta.

Also, a few other options you can throw in when invoking GCC.
* -O0, -O1, -O2, -O3 - For specifying different levels of optimization.
* -g0, -g1, -g2, -g3 - For specifying the debugging level.
* -std=<standard> - For specifying the standard of C you're compiling. I usually use "c99".
* -Wall - Display all warnings.
* -Werror - Treat warnings as errors.
* -pedantic-errors - Much more strict about finding errors. IMO this forces you to write better code.

And there are many more, those are just the ones I could think of.
nitin_c_c++
GCC - GNU compiler Collection is the best compiler ever i have worked.

It is available for 8 bit micro controller to 64 bit Processor, for all kind of processors like intel, motorolla, itanium, arm, atmel, sparc, powerpc etc.
It provide cross compilation and several features which is unique, no one have.
I will say this is the strongest and biggest compiler in all available compiler.
It can compile C, C++, Objective-C, Fortran, Java, and Ada kind of language.

This is the compiler which is being used in Desktop, Micro Computer, Mainframe to Super Computer.

It is simple to use once u start using it, will find very easy.

For more options and information about GCC compilation, shared lib u can go following link GCC-A Great Compiler
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.