Languages and Scripting
Showing results for 
Search instead for 
Did you mean: 

dynamic library problem (shl_load)

Go to solution
Ulrich Deiters
Frequent Advisor

dynamic library problem (shl_load)

Condider the following case:
file1.c contains the main program and
some subroutines. file2.c contains subroutines only, which need to go into a shared library. I compile like this:
cc -Aa -c +z -g file2.c
ld -b -o file2.o
cc -Aa -g file1.c -ldld
The mainprogram loads libxyz by means of shl_load.
If I run a.out and file2.c contains external variables (extern int something ...), the shl_findsym fails. Without external variables the subroutines from file2 are loaded correctly - but then it is not possible to invoke a subroutine from file1 any longer (dld error).

The dlopen method works, but that is not available on my system (HP-UX 10.20).

Any ideas what went wrong?
Manish Srivastava
Trusted Contributor

Re: dynamic library problem (shl_load)


Could you give the dld error message.

Ulrich Deiters
Frequent Advisor

Re: dynamic library problem (shl_load)

... something like "dld error, symbol not found.

This is funny, because the missing subroutine had been linked statically.
Mike Stroyan
Honored Contributor

Re: dynamic library problem (shl_load)

A declaration like
extern int my_symbol;
does not allocate any storage for "my_symbol".
If you want the shared library to provide a
symbol you need to declare it as
int my_symbol;
somewhere in the shared library.

If you meant to declare a symbol in the a.out
and access it from the shared library, then
you may need to add a -Wl,-E option when
linking the a.out. That tells ld to export
all symbols for use by shared libraries even
if they are not used by code in the a.out
Ulrich Deiters
Frequent Advisor

Re: dynamic library problem (shl_load)

The exact error message was
"/usr/lib/ Unresolved symbol: parse_global (code) from /home/ukd/architex
where parse_global() is a statically linked
subroutine (or rather, it is compiled together with main()).

I do not quite understand why the program forgets its static subroutine table. But whatever the reason is,"-Wl,-E" did the trick! Thank you for your help.
ranganath ramachandra
Esteemed Contributor

Re: dynamic library problem (shl_load)

by default, symbols are not exported from the application program ('a.out') unless they are being imported by a shared library seen at link-time (not one that may dynamically loaded by the user). so in your case the precise solution is to add this to your compile line : '-Wl,+ee,parse_global'

the -E flag makes the linker export all symbols from a.out, that is how it fixed your problem.
(i work for hpe)

Accept or Kudo