Search This Blog

Tuesday, May 19, 2009

How to create & use a .so (Shared Object) file.

STEP1 Create a C file having a function.
eg fun_with_so.c
=============================================
#include
extern void fun_from_so(void);
void fun_from_so()
{
printf("This is function called from so");
}
===========================================

STEP2 Create a C file that uses function from shared object Library i.e using fun_with_so()
eg using_so.c
============================================
#include
main()
{
fun_from_so();
}
=============================================

STEP3 Command to create .so file of fun_with_so.c
$ gcc -o fun_with_so.so -shared fun_with_so.c
This will create fun_with_so.so file.
Copy fun_with_so.so ro /lib or in includes

STEP4 Command to complie & linking C file with .so file
$ gcc using_so.c fun_with_so.so
This will create an a.out binary. [ you can change name with -o option]

STEP5 OUTPUT run binary on terminal............
[root@vikas so]# ./a.out
This is function called from so[root@vikas so]#

No comments:

Post a Comment