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
: 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






MultiQuote



|