Operating System - HP-UX
1828220 Members
2094 Online
109975 Solutions
New Discussion

Re: creating shared library with 'cc' instead of 'ld'

 
steve flynn_1
New Member

creating shared library with 'cc' instead of 'ld'

Hello there,

I am attempting to create a shared library using the cc comand;

cc -Wl,-b,-I -o .o

Having compiled the source using the '+Z' option.

The linker, of its own accord links, the file '/opt/langtools/lib/crt0.o' and then complains

that this is not position independent code! Suggested recompile using '+z' or '+Z'.

Does anybody have any ideas ?

steve
3 REPLIES 3
kalimuthu
New Member

Re: creating shared library with 'cc' instead of 'ld'

Hello steve,

You mean that ,All source are compiled with +z option, then

you could use the following command to create the shared library.

ld -b -o .c

eaxmple:

# cat sdisplay.c
#include
void sdisplay()
{
printf("Message from static lib\n");
}


# cat ddisplay.c
#include
void ddisplay()
{
sdisplay();
printf("Message from dynamic lib\n");
}

# cat main.c
#include
int main()
{
ddisplay();
printf("Message from main");
return 0;
}

# cc -c sdisplay.c
# ar cq libsdisplay.a sdisplay.o
# cc +z -c ddisplay.c
# ld -b -o libddisplay.so ddisplay.o\
-L. -lsdisplay
# cc main.c -L. -lddisplay
# ./a.out

Message from static lib
Message from dynamic lib
Message from main

First created dynamic object with +z option, use ld to create the dynamic library with static.

I hope this will work for your problem.

regards
kalimuthu
Manish Srivastava
Trusted Contributor

Re: creating shared library with 'cc' instead of 'ld'

Hi,

I just tried to build few object files with +Z and then use your compile line to create a library and was able to do so.

1. cc +Z -c a.c b.c
2. cc -Wl,-b,-I -o lib.so a.o b.o

crt0.o is required to build the final binary and is invoked by the linker please have a look at ld manpage.

manish
ranganath ramachandra
Esteemed Contributor

Re: creating shared library with 'cc' instead of 'ld'

the crt0 is passed to the linker by the compiler, it is used only when building an executable. the linker does very few things by itself (like using milli.a).

i dont understand why your compile options are like this: -Wl,-b,-l. what is '-l' doing there ?

do not pass the '-b' option to the linker, put it in the compile options. the compiler will understand that '+z' is to be used. it will pass on the '-b' to the linker as well. it will also NOT add the crt0 to the link line.

if you dont say '-b' to the compiler, it will assume you are building an executable and add crt0 to the link line.
 
--
ranga
hp-ux 11i v3[i work for hpe]

Accept or Kudo