Welcome to Dream.In.Code
Getting Help is Easy!

Join 99,788 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,566 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Intro to the GNU C Compiler

 
Reply to this topicStart new topic

> Intro to the GNU C Compiler, Getting started programming in Unix

Nova Dragoon
Group Icon



post 28 Jul, 2005 - 01:46 PM
Post #1


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


Register to Make This Ad Go Away!

Tom9729
Group Icon



post 4 Jan, 2008 - 11:48 PM
Post #2
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++
Group Icon



post 26 Jun, 2008 - 03:41 AM
Post #3
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 post has been edited by nitin_c_c++: 26 Jun, 2008 - 04:07 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 7/25/08 01:42AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->