Operating System - HP-UX
1753803 Members
7435 Online
108805 Solutions
New Discussion юеВ

What is the commad to make a sharable lib?

 
SOLVED
Go to solution
Ai Jun Zhang
Occasional Contributor

What is the commad to make a sharable lib?

Hi!

I do not know if cc can make a sharable lib.
If it is, what is the option I should put in?

Please let me know.

Thanks.
Aijun.
5 REPLIES 5
RikTytgat
Honored Contributor
Solution

Re: What is the commad to make a sharable lib?

Hi,

From the ld(1) manpage:

"-b Create a shared library rather than a normal executable file. Object files processed with this option must contain position-independent code (PIC). See the discussion of PIC in cc(1), CC(1) (part of the optional C++ compiler documentation), f77(1), pc(1), as(1), and Linker and Libraries Online User Guide."

So, first you have to compile your source without linking it. To do so, use the cc(1) switch 'c. For example:

cc -c mylib.c -o mylib.o +z
ld mylib.o -o mylib.1 -b

The +z switch to cc means:
"Causes the compiler to generate position-independent code (PIC), necessary for building a shared library."

This should do the trick.

Bye,
Rik
Ai Jun Zhang
Occasional Contributor

Re: What is the commad to make a sharable lib?

Hi! Rik,

Thank you very much for your help. I made my
shared library as you told me. It is called
testlib.1

Now I have a test.c, which calls the function in the testlib.1, I already made my test.o successfully. But when I try to make an exe, it gives me error as follows:


/home/azhang:cc -o test.o testlib.1
/usr/ccs/bin/ld: Unsatisfied symbols:
main


Please let me know how to make an executable when an sharable lib is used.

All the three files testlib.c testlib.h and test.c are in the same directory.

For your reference, my code is extremely simple.

testlib.h contains one line of code.
----------
int getint();

testlib.c is
-------------
#include "testlib.h"

int getint()
{
return 5;
}

test.c is
-----------
#include
#include

main()
{

int n;

n = getint();

printf("result is %dn", n);
}


Thanks.
Aijun.
RikTytgat
Honored Contributor

Re: What is the commad to make a sharable lib?

Aijun,

My example was not completely accurate. You should name your library libtest.1

What you need to do next is

cc test.c -o test -L -ltest

The cc command will add the 'lib' in front of the 'test' to build the name libtest

This takes care of putting the right libraries in the library list for your executable. You can always check the built-in library paths by using the command 'chatr '. Have a try with any executable and have a look at the output.

Good luck,
Rik
Ai Jun Zhang
Occasional Contributor

Re: What is the commad to make a sharable lib?

Hi! Rik,

I made the libtest.1, but when I try to produce my exe as you suggested. It gives me
error as following.


:cc -o test.exe test.o -L. -ltest
/usr/ccs/bin/ld: Can't find library for -ltest


The libtest.1 is there in the local directory. See the following.
/home/azhang:ls libtest.1
libtest.1

As I told you before, all the files are in the local directory.

Can you let me know what can be wrong?

Thanks.
Aijun.
RikTytgat
Honored Contributor

Re: What is the commad to make a sharable lib?

Aijun,

I forgot a little detail. The name of the library must be libtest.sl

To solve the problem, don't rebuild your library with the .sk extension. Instead, create a symbolic link by executing the command:

ln -s libtest.1 libtest.sl



The reason to create libtest.1 is to support shared library versioning. When you build your executable, the linker will look for the .sl lib, but the .1 name will appear in the library list. (check with chatr). This is the way HP manages to run 10.20 executables on 11.00. They provide 2 sets of libraries. I.e libc.1 (for executables built on 10.20) and libc.2 (for executables built on 11).

This should solve your problems (provided I didn't forget anything ;-))

Bye,
Rik