Operating System - HP-UX
1748259 Members
4064 Online
108760 Solutions
New Discussion юеВ

Re: Need to compile a shared library with gcc-2.95.2 on HP-UX

 
Avinash V. Sarlashkar
Occasional Advisor

Need to compile a shared library with gcc-2.95.2 on HP-UX

Hello:
I have HP-UX 10.2 running on C110 machine. I have the bundled HP-C compiler as listed below.
_____________________________________________
#what /usr/ccs/bin/cc
/usr/ccs/bin/cc: HP92453-01 A.10.32.00 HP C (Bundled) Compiler
/usr/lib/libc: $Revision: 76.3 $
_____________________________________________

I have gcc-2.95.2 properly installed on the machine. I am trying to create a "shared" library using gcc. The object modules are produced from a bunch of *.cpp (c++) files using "gcc -c -fpic *.cpp". However, when I try to link them with the following command
"gcc -shared *.o -o x.sl", I get the following message:
___________________________________________
/usr/ccs/bin/ld: DP relative code in file /var/tmp/cc4cnn5b.o - shared
library must be position independent. Use +z or +Z to recompile.
collect2: ld returned 1 exit status
___________________________________________
I recognize that the +z or +Z are the options on the HP ANSI C compiler and not gcc.

If I link explicitly with "ld" (ld -b *.o -o x.sl), it creates a library, however, it seems it is incorrectly built. As a matter of fact, in the HP documentation, it is explicitly stated that "ld" should not be used to generate shared libraries with c++ code, but rather cc should be used.

Any pointers on what I am doing wrong or what I need to do differently?

Thanks for your time.

Avinash
5 REPLIES 5
Stefan Farrelly
Honored Contributor

Re: Need to compile a shared library with gcc-2.95.2 on HP-UX


Tricky problem.

I wouldnt be too concerned about the HP docs saying your should use cc to create a library for c++ code. Sounds like a marketing ploy to me, so that HP require you to load/buy the HP c++ compiler in addition/instead of the Ansi/C compiler. As you pointed out, using gcc you can create the object files just fine, and ld creates the library fine, so no problem. You cant use gcc to create the library direct because ld expects an HP compiler, not the gnu one.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Avinash V. Sarlashkar
Occasional Advisor

Re: Need to compile a shared library with gcc-2.95.2 on HP-UX

Stefan:

Thanks for your input. Is there a way to check for "integrity/correctness/consistency" of the shared library that I created with explicitly using "ld" ? The following quote is directly from the "Compiler-Linker Interaction" section of the HP manual "http://docs.hp.com/hpux/onlinedocs/B2355-90655/B2355-90655.html". My concern stems from the statement that "you may not get any error messages". I am not sure whether my failure in using the shared library is a result of the what is stated below or something else. Hence my question above regarding "correctness". Thanks for your time.

Regards
Avinash
______________________________________________
If you are linking any C++ object files to create an executable or a shared library, you must use the CC command to link. This ensures that c++patch executes and chains together your nonlocal static constructors and destructors. If you use ld, the library or executable may not work correctly and you may not get any error messages. For more information see the HP C++ Programmer's Guide .
_____________________________________________
Andy Bennett
Valued Contributor

Re: Need to compile a shared library with gcc-2.95.2 on HP-UX

The message in the linker manual should be read as "Use the compiler that built the code to link it into a library". The reason is that, as the NOTE says, most C++ compilers need to add extra steps to the link phase to get static constructors/destructors to fire correctly.

This is exactly what gcc is trying to do. In order to do this it compiles a C fix up module and then adds that to your library link. It must build this as PIC which it currently isn't and ld is consequently complaining about it. Therefore, add "-fpic" to the link line so that when it propagates the arguments down it knows to do this. That is:

gcc -fpic -shared *.o -o x.sl


Andy.
Kevin Ernst
Regular Advisor

Re: Need to compile a shared library with gcc-2.95.2 on HP-UX

I'm kind of a dummy when it comes to compiler internals, but let me just throw this in... I've seen suggested in several places to use the GNU binutils ('ar,' 'gas,' 'ld,' and friends) in conjuction with gcc to avoid problems. Binutils is available in binary form (as a depot) from an HP-UX Porting Archive near you. The Central US mirror site is http://hpux.cae.wisc.edu .
Avinash V. Sarlashkar
Occasional Advisor

Re: Need to compile a shared library with gcc-2.95.2 on HP-UX

Andy and Kevin:

Thanks for your input. Will post any progress I make in this regard.

Avinash