You are here:

C/how will you create own library...

Advertisement


Question
how will you create own library functions
in c language?
can you explain with example?

Answer
Suppose, you have 2 files send.c and get.c, which are part of your project.
Now, you want to create a library out of these, which will be named as libMyLibrary.so.
Herre are the steps to do that:

gcc -fPIC -c send.c
gcc -fPIC -c get.c
ld -shared -o libMyLibrary.so -lc send.o get.o

After executing these three statements, you will get libMyLibrary.so, which is a dynamic library.

Now, you want to use the functions defined in this library in your test program test.c
To compile that you can use the command:
gcc -L./ -o my_test test.c -lMyLibrary

The -L./ flag is to tell the linker to search for the library file in the current path also (along with standard path). The reason is that, your library is in current path.
If it is in a different path, then use it with -L flag.

Hope this helps.....

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.