Starting with C
From Initq
There are a few things you need to know before you get cracking with C programming.
Contents |
Code
The actual code of your program consists of two parts: variables and executable instructions. Variables are used to hold the data used by your program. Executable instruction tell the computer what to do with the data.
Sample Code
Here is a simple Hello World program.
#include <stdio.h>
int main()
{
printf("Hello World\n");
exit(0);
}
To compile and run the above code you will do
[lexiana@initq 1]$ gcc -o hello hello.c [lexiana@initq 1]$ ./hello Hello world
It is customary to put your applications in either /usr/local/ or /opt.
You compiler is normally located in /usr/bin/gcc.
Header files
Header files provide definitions of constants and declarations for system and library functions. These are located at /usr/include You include these files in your code by using the -I flag
- $ gcc -I/usr/openwin/include fred.c
Library files
Libraries are collection of precompiled functions. They are located at /lib or /usr/lib. These files start with lib and c for C Library and m for mathematical library, they end with .a for static libraries and .so for shared libraries. To compile
- $ gcc -o fred fred.c /usr/lib/libm.a
or
- $ gcc -o fred fred.c -lm
Static libraries
first we will create two source files.
#include <stdio.h>
void fred (int arg)
{
printf("fred: you passed %d\n", arg);
}
#include <stdio.h>
void bill (char *arg)
{
printf("bill: you passed %s\n", arg);
}
Now we will make object files.
[lexiana@initq 1]$ gcc -c fred.c bill.c [lexiana@initq 1]$ ls *.o bill.o fred.o
Now we write a header file that calls the two functions.
void bill(char *); void fred(int);
now we write our main program
#include <stdio.h>
#include "lib.h"
int main()
{
bill("hello world");
exit(0);
}
We compile it all and run.
[lexiana@initq 1]$ gcc -c program.c [lexiana@initq 1]$ gcc -o program program.o bill.o [lexiana@initq 1]$ ./program bill: you passed hello world [lexiana@initq 1]$
Now let's create and use a library. (c = create, r = addfiles)
[lexiana@initq 1]$ ar crv libfoo.a bill.o fred.o a - bill.o a - fred.o
To use the library a table of contents has to be created.
[lexiana@initq 1]$ ranlib libfoo.a [lexiana@initq 1]$ gcc -o program program.o libfoo.a [lexiana@initq 1]$ ./program bill: you passed hello world
you can also use this
[lexiana@initq 1]$ gcc -o program program.o -L. -lfoo
To check what functions are included in libfoo.a
[lexiana@initq 1]$ nm libfoo.a
bill.o:
0000000000000000 T bill
U printf
fred.o:
0000000000000000 T fred
U printf
When we use many applications that use the same static libraries results in having the same libraries in memory many time over. To avoid this we use shared libraries.
Shared libraries are stored in the same place as static libraries with extension .so.
Shared libraries are good when doing updates because no change is needed for the application itself. This is because all the libraries are symbolicly linked. example /usr/lib/libm.so will be linked to /usr/lib/libm.so.N where N will be a major version number.
We use a program called ldd to see what shared libraries are used by a program. For the over all shared library for the system a file is used to store the information. This file is /etc/ld.so.conf and is updated by a the program ldconfig.
[lexiana@initq 1]$ ldd program
linux-vdso.so.1 => (0x00007fffdddff000)
libc.so.6 => /lib64/libc.so.6 (0x00007f14d56a7000)
/lib64/ld-linux-x86-64.so.2 (0x00007f14d5a08000)