Archive

Archive for the ‘PROGRAMMING’ Category

How to create shared libraries with gcc?

May 17th, 2008 1 comment

gccShared libraries are libraries that are loaded by programs when they start. All programs that start afterwards automatically use the new shared library.

Code for the library:

This is the code that goes into the library. A function that calculate and return the factorial of a number.

fact.c

int
fact (int n)
{
        int fact  = 1;
        if(n == 0 || n == 1)
                return 1;
        while(n > 1) {
                fact = fact * n;
                n--;
        }
        return fact;
}

Now create a header file to define our function.

fact.h
int fact (int);

Creating Shared library:

First, create the object files that will go into the shared library using the gcc -fPIC or -fpic flag. The -fPIC and -fpic options enable “position independent code” generation, a requirement for shared libraries.

$ gcc -fPIC -Wall -g -c fact.c

Every shared library has a special name called the “soname”. The soname has the prefix “lib”, the name of the library, the phrase “.so”, followed by a period and a version number.

We can use ‘ld’, the GNU linker to create our shared library. The ld combines a number of object and archive files, relocates their data and ties up symbol references.

$ ld -shared -soname libfoo.so.1 -o libfoo.so.1.0 fact.o

Installing Shared library:

Once you’ve created a shared library, you’ll want to install it. The simple approach is simply to copy the library into one of the standard directories (e.g., /usr/lib) and run ldconfig(8).

Now copy the libfoo.so.1.0 to /usr/lib

$ sudo cp libfoo.so.1.0 /usr/lib/
$ sudo ldconfig -n /usr/lib/

Now create a symbolic link to our library.
$ sudo ln -sf /usr/lib/libfoo.so.1 /usr/lib/libfoo.so

Linking our Shared library:

This is the program that uses our ‘foo’ library.

main.c

#include <stdio.h>
#include "fact.h"
 
int
main(int argc, char *argv[])
{
        printf("%d\n", fact(4));
        return 0;
}

Compile:

$ gcc main.c -lfoo -o fact

Now run program:

$ ./fact
24

Categories: PROGRAMMING Tags:

How to create static libraries with gcc?

April 25th, 2008 No comments

gccStatic library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. Here I will try to explain how to create a static library with gcc.

Code for the library:

First we write some code for our library. The below function will find and return the factorial of a number.

fact.c

int
fact (int f) {
  if ( f >= 1 )
    return f;
  return f * fact ( f - 1 );
}

We need to create a header file to define our function.

fact.h
int fact (int);

Creating static library:

Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ‘.a’‘ .
To generate object file use ‘-c‘ flag with gcc.

$ gcc -c fact.c -o fact.o

Flags:
-c Compile or assemble the source files, but do not link. The compiler output is object files corresponding to each source file.
-o To specifiy the output file name.

A static library can contain more than one object files. We need to copy all object file in to a single file. This single file is our static library. We can use archiver(ar) to create to create our static library.

$ ar rcs libfact.a fact.o

Options:

r – Insert the files into archive (with replacement).
c – Create the archive.
s – Write an object-file index into the archive, or update an existing one, even if no other change is made to the archive.

Note: the library must start with the three letters lib and have the suffix .a.

A program using the library:

main.c

#include <stdio.h>
#include "fact.h"
int
main(int argc, char *argv[])
{
        printf("%d\n", fact(4));
        return 0;
}

Linking our static library:

$ gcc -static main.c -L. -lfact -o fact

Options:

-L Add directory to the list of directories to be searched for -l.

Now run program:
$ ./fact
24

Categories: PROGRAMMING Tags: