School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,127 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,049 people online right now. 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 - 12: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
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Tom9729
Group Icon



post 4 Jan, 2008 - 10: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.
Go to the top of the page
+Quote Post

nitin_c_c++
Group Icon



post 26 Jun, 2008 - 02: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 - 03:07 AM
Go to the top of the page
+Quote Post


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: 11/21/09 02:27PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month