Operating System - HP-UX
1827454 Members
5507 Online
109965 Solutions
New Discussion

Re: Create shared library

 
SOLVED
Go to solution
Patrick Chim
Trusted Contributor

Create shared library

Hi,

How can I implement a C program to a shared library so that any other C program can call the function in the shared library ?

Regards,
Patrick
7 REPLIES 7
glenn henley
Occasional Advisor

Re: Create shared library

If I understand your question correctly
a compiler line something like :-

aCC -b +z -o main.sl main.cpp

should suffice
The only people that never make mistakes, are the people that never try anything.
Patrick Chim
Trusted Contributor

Re: Create shared library

Hi,

How can I compile another program with this shared library ? Can you show the command line for me ?

Regards,
Patrick
glenn henley
Occasional Advisor

Re: Create shared library

Here's about the most simplistic example
I can come up with.... normally I'd have
stuff like this in a makefile.

stuff.h
-------
#include
void out();

stuff.cpp
---------
#include "stuff.h"
void out()
{
printf("Hello world\n");
};

main.cpp
--------
#include "stuff.h"
int main()
{
out();
return 1;
}

..and then to actually build the things
---------------------------------------

1) aCC -b +z -o libStuff.sl stuff.cpp
2) aCC -L./ -lStuff -o main main.cpp

-L indicates where 'Stuff' lives
-l is the library name, the lib & .sl
gets wrapped around it.

I think that's it.... just make sure if you
try this example everything lives in the
same directory.

Best 'o Luck

Glenn
The only people that never make mistakes, are the people that never try anything.
Patrick Chim
Trusted Contributor

Re: Create shared library

Hi glenn,

My system running HP-UX 11.0 and use the cc compiler come from HP. I replace the aCC with cc but it say it does not have the -b option. Do you know how to compile in my environment ?

And, can I add the shared library in a search path ? If so, what environment variable can I set ?

Many thanks,
Patrick
Klaus Crusius
Trusted Contributor

Re: Create shared library

Hi,

with the ANSI-C compiler (the bundled cc compiler which comes with HPUX does not support Positin Independant Code), the procedure would be :

cc +z -c -o sub.o sub.c
ld -b sub.o libxxx.sl
mv libxxx.sl $instdir

cc -L $instdir main.c -o main

Regards, Klaus
There is a live before death!
Blanche
Occasional Advisor
Solution

Re: Create shared library

to create your library

for each source file :
cc +z -c file.c

and then
ld -b -o libmylib.sl file1.o file2.o ....

to use it in building an executable :
cc -L. -lmylib -o myexec myexec.c

you should set the SHLIB_PATH environment variable
when you launch the program.

Blanche
Blanche
Occasional Advisor

Re: Create shared library

Sorry I fogot an option to use the SHLIB_PATH variable at run-time

the right compilation line is :
cc -Wl,+s -L. -lmylib -o myexec myexec.c

Blanche