Page 1 of 1

Intro to the GNU C Compiler Getting started programming in Unix Rate Topic: -----

#1 Nova Dragoon  Icon User is offline

  • The Innocent Shall Suffer, Big Time
  • member icon

Reputation: 36
  • View blog
  • Posts: 6,169
  • Joined: 16-August 01

Posted 28 July 2005 - 01:46 PM

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.
#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.


#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

Is This A Good Question/Topic? 1
  • +

Replies To: Intro to the GNU C Compiler

#2 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Posted 04 January 2008 - 11:48 PM

Nice tutorial. :^:

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.
Was This Post Helpful? 1
  • +
  • -

#3 nitin_c_c++  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 18-June 08

Posted 26 June 2008 - 03:41 AM

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 June 2008 - 04:07 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1