Create a lib from a c program

I have a function in a c program that I want to to share with other programs.

How do I create a lib using the cc compiler ?

I think that it might be possible to create a shared library in a single step if it contained only one module. But libraries usually contain several modules. Each .c file is compiled seperately. Then they are all combined into a shared library with a single invocation of ld.

But the exact options depend on what os, what release, what compiler, etc.

What I am looking for is the standard c way for creating libs in unix, as standard as possible.

The os I am using is "SunOS 5.6"

this link is for AIX, but I used the procedure to test out on my RHL 7.3 and it works fine... so I think you should not have problem save a little exercise for you to find the appropriate option for your cc implementation...

http://www.faqs.org/faqs/aix-faq/part4/section-1.html

Cheers!
Vishnu.

I'm not sure, but I'll take a guess. I am also assuming that:
you are using /opt/SUNWspro/bin/cc for your compiler.
you are using a 32 bit address space
you are using a sparc chip
but you are not using a sparc v9

If those assumptions are correct, it looks like this might work...

cc -KPIC x.c
cc -KPIC y.c
cc -KPIC z.c
ld -G -Bdynamic -o libxyx.so x.o y.o z.o

I don't have a decent compiler on a 5.6 so I can't test this. But it's close. In your case, you say you have only a single module, so you would have only one cc. But that is very rare with a library. So I'm showing a more normal case where you would combine several modules into a library.

To generalize this more use on many versions on unix, you typically would use a makefile. The "-KPIC" would be in a variable so it could be changed as you moved from one version to another. Ditto for the "-G -Bdynamic".

This link is the Linker and Libraries Guide, which you might want to look at. Also look at the man page for your compiler. Sun has several things called cc so you need to get the MANPATH right or you will be looking at the wrong one.

If this doesn't work, try replacing -KPIC with -Kpic.

Thank you very much for your help, I think I've got it now.

For the record ,this is a simple example of how I did it:

1) Create ut.c with the func to include in the lib

int F1( int i) {
printf("Print N� = %d\n", i);
}

2) Complie ut.c to create ut.o

cc -c ut.c

3) Create Lib from ut.o

ar r libut.a ut.o

4) Create t1.c to invoke the func in lib

void main ( int argc, char *argv[]) {
int i_in;
if ( argc != 2) {
printf( "Wrong parameters, use : t1 <n�> \n");
return;
}
i_in = atoi( argv[1]);

F1( i_in);
}

5) Complie t1 linking it to the lib created in 3)

cc -o t1 t1.c -L: libut.a

And it Works :
>t1 69
Print N� = 69

Hi Perderabo,

I couldn't access the link "Linker and Libraries Guide" given by you.
Don't know if i am alone facing that problem. Could you please check it out and provide me the link..

Thanks

Me too!

This is what I get !

Not Found
The requested object does not exist on this server. 
The link you followed is either outdated, inaccurate, 
or the server has been instructed not to let you have it. 
Please inform the site administrator of the referring page.

Here's an archive.org cache of it, hopefully Sun still has a live version somewhere..

They seem to have subsumed it into the documentation on various different Solaris versions. here is the version in the Solaris 10 docs, other versions can be found with this google search.