Tuesday, October 29, 2013

How to create your own library code in C


In general, libraries are created from many library source files, and are either built as archive files (libmine.a) that are statically linked into executables that use them, or as shared object files (libmine.so) that are dynamically linked into executables that use them.

To link a library file into your code, you can use the gcc command line option -L for the path of the library files and -l to link a library file, for example:
$ gcc -o a.out tony.c -L /home/tony/lib -ltony

You may also need to specify and include path so the compiler can find the library header file: -I /home/tonyinclude

This tutorial walk you through creating and using your own library files.
This tutorial also provides your a bitmap C implementation:

1. Create an INTERFACE to your library: bitmap.h
/* bitmap.h in c
 * author: Tony
 * Time: 2013-10-29
 */

#ifndef _BITMAP_H_
#define _BITMAP_H_

/* Initialize bitmap
 * size: the size of bitmap
 * start: start value
 */
int bitmap_init(int size, int start);

/* Set the value of ith position */
int bitmap_set(int index);

/* Wether the given index is in bitmap */
int bitmap_get(int i);

/* Get the value */
int bitmap_data(int index);

/* Release memory */
int bitmap_free();

#endif /* INCLUDED_BITMAP_H */
2. Create an implementation of the library file: bitmap.c
/* Last modified Time-stamp: <Tony 2013-10-25 11:18:58>
 * @(#)bitmap.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bitmap.h"

unsigned char *g_bitmap = NULL;
int g_size = 0;
int g_base = 0;

int bitmap_init(int size, int start){

    g_base = start;
    /* How many chars in bitmap */
    g_size = (size/8) + 1;
    g_bitmap = (char *)malloc(g_size*sizeof(char));

    /* Memory allocation failed */
    if (g_bitmap == NULL) return 0;

    /* set all elements to 0 */
    memset(g_bitmap, 0x0, g_size);
    return 1;
}


int bitmap_set(int index){
    int quo = (index-g_base)/8;
    int remainder = (index - g_base)%8;
    unsigned char x = (0x1<<remainder);
    if (quo > g_size)
    {
        return 0;
    }
    g_bitmap[quo] |= x;
    return 1;
}

int bitmap_get(int i) {
    int quo = (i)/8;
    int remainder = (i)%8;
    unsigned char x = (0x1<<remainder);
    unsigned char res;
    if (quo > g_size) return -1;
    res = g_bitmap[quo] & x;
    return res > 0 ? 1 : 0;
}

int bitmap_data(int index) {
    return (index + g_base);
}

int bitmap_free() {
    free(g_bitmap);
}

3. Create a test file to: bitmap_test.c
/* Last modified Time-stamp: <Tony 2013-10-29 16:29:58>
 * @(#)itmap_test.c
 */

#include <stdio.h>
#include "bitmap.h"

int main() {
    int a[] = {5,8,7,6,3,1,10,78,56,34,23,12,43,54,65,76,87,98,89,100};
    int i;
    bitmap_init(100,0);
    for(i=0; i<20; i++) {
        bitmap_set(a[i]);
    }

    for(i=0; i<100; i++) {
        if(bitmap_get(i) > 0) {
            printf("%d ", bitmap_data(i));
        }
    }

    printf("\n");
    bitmap_free();
    return 0;
}

4. Create a LIBRARY OBJECT FILE that can be linked with programs that want to use our library code
$ gcc -o bitmap.o -c bitmap.c

5. Compile bitmap_test.c with the new library file:
$ gcc bitmap.o bitmap_test.c -o bitmap

6. Execute bitmap the executable:
$ ./bitmap
1 3 5 6 7 8 10 12 23 34 43 54 56 65 76 78 87 89 98

No comments: