How to create a library [Archive] - C Board

PDA

View Full Version : How to create a library


Unregistered
03-13-2002, 06:19 AM
Hi there,
I'm a newbie in C and I made some functions, but now I wanna use them as libraries.
I create a function corr() in a file corr.c and corr.h and compile that with:
gcc -c corr.c -o corr.o. Now everytime I need to use my function on a program I have to write include the line #include <corr.h> and then type

gcc corr.o myprog.c -o myprog

I want to know if there is any way that I needed to include the line #include <corr.h> in my program and I only had to type:

gcc myprog.c -o myprog

in order to compile?
I apreciate if anyone could send me a link where I can find the info I need or if anyone can explain how do I do that.

Thanks for any help!
Fernando

rohit
03-13-2002, 10:43 PM
even when you have the library with you you can not do
gcc myprog.c -o myprog

to generate myprog binary you will have to link it with your library.


gcc -o myprog -L/usr/lib/yourlibrary -I/yourincludepath myprog.c



cheers
Rohit

M-21
03-22-2002, 08:43 PM
I have the same question.
How do I made my own library, so I could use it like:

#include <stdio.h>
#include <mylib.h>

without specifying the library path (you don't have to specify
the path for 'stdio.h', do you)?

sigma
03-27-2002, 01:14 AM
This is the syntax for IRIX 6.5. It should be similair for your version of linux.

To compile a library:
cc -shared corr.c -o libcorr.so

This will create a shared object. you will need to place the file in a directory specified in your LD_LIBRARY_PATH environment variable.

To compile the executable:
cc myprog.c -lcorr -o myprog

The option -lxxx links in objects from the libxxx.so or libxxx.a (in that order) from directories in the LD_LIBRARY_PATH

This is just like when you use math.h, you have to include the -lm option when you compile.