1 to create some .c file and .h file, for example, we create four .c file and one .h file.
/*a.c file*/
#include<stdio.h>
void function_test1(void)
{
printf("hello, this is [%s]\n", __FUNCTION__);
return;
}
/*b.c file*/
#include<stdio.h>
void function_test2(void)
{
printf("hello, this is [%s]\n", __FUNCTION__);
return;
}
/*c.c file*/
#include<stdio.h>
void function_test3(void)
{
printf("hello, this is [%s]\n", __FUNCTION__);
return;
}
/*abc.h file*/
#include <stdio.h>
void function_test1(void);
void function_test2(void);
void function_test3(void);
/*main.c file*/
#include<stdio.h>
#include"abc.h"
int main(void)
{
function_test1();
function_test2();
function_test3();
return 0;
}
2 create library object files and shared librarygcc -fPIC -Wall -g -c a.c b.c c.c /*to create object file*/
gcc -g -shared -Wl,-soname,libtest.so.1 -o libtest.so.1.0.1 a.o b.o c.o -lc /*to create shared library*/
3 set up the soname and linker nameln -sf libtest.so.1.0.1 libtest.so.1 /*set up the soname*/
ln -sf libtest.so.1 libtest.so /*set up the linker name*/
4 figure out the path of shared library
copy the library libtest.so.1.0.1, libtest.so.1,libtest.so to /usr/local/libthen
use the ldconfig to figure out the path /sbin/ldconfig -n
5 compile the file main.c which use the shared library
gcc -Wall -g -c main.c -o main.o
gcc -g -o main main.o -L. -ltest
if you want put the shared libarary in a temp dir: home/hero/program/libdir, use the following link option:
gcc -g -o main main.o -L /home/hero/program/libdir -ltest
6 Execute the program main
./main