Wednesday, July 8, 2015

how to make library

You probably have two files named 'mylib.h', 'mylib.c'.
Let's make library by these files.
step 1. compile 'mylib.c' to make object file.
    [to make static library] gcc -c mylib.c
    [to make shared library] gcc -fPIC -c mylib.c
step 2. create library from object file.
    [to make static library] ar rc libmylib.a mylib.o
    [to make shared library] gcc -shared -o libmylib.so mylib.o
step3. In the case of static libraries, create the index in libmylib.a library.
    ranlib libmylib.a

Usage of created library.
compile the main file by following command
   gcc main.c -o run.out -l[location of mylib.c] -L[location of libmylib.a(so)] -lmylib 

If you specify the PATH in advance, -l[PATH] and -L[PATH] could be skipped.
In the case of bash shell: open .bashrc file in the home directory, insert
    export C_INCLUDE_PATH=/homes/INCLUDE
    export LIBRARY_PATH=/homes/LIBRARY
In the case of TC shell: open .tcshrc file in the home directory, insert
    setenv C_INCLUDE_PATH /homes/INCLUDE
    senenv LIBRARY_PATH /homes/LIBRARY
then compiler may automatically find the header files from /homes/INCLUDE, and library files from /homes/LIBRARY.

Reference: http://randu.org/tutorials/c/libraries.php

No comments:

Post a Comment