lhw
lhw

Reputation: 548

Get dlopen to ignore undefined symbols

I am compiling a dynamically generated C++ file as shared object which contains references to symbols available only in it's full build.

g++ -o tmp_form.so -fPIC -shared -lsomelib -std=gnu99 tmp_form.cc

I don't need the missing symbols for my current program, only those from the linked library. But dlopen does require them to be available or fails otherwise. The missing symbols are all variables which are being referenced in structs.

One option would be to add the weak reference attribute to the missing symbols in the generated code. But I would like to avoid making changes to the code generator if possible.

Any advise is appreciated.

Upvotes: 1

Views: 2061

Answers (2)

Employed Russian
Employed Russian

Reputation: 213879

Your link command is incorrect:

... -lsomelib ... tmp_form.cc

should be

... tmp_form.cc -lsomelib

The order of sources/objects and libraries on the link line does matter.

If you are using an ELF platform and a very recent build of Gold linker, you can "downgrade" unresolved symbols to weak with --weak-unresolved-symbols option (added here) without modifying the source.

Otherwise, you'll have to modify sources, there is no other way.

P.S. Function references would not have a problem with RTLD_LAZY due to lazy binding, but for data references weak unresolved is your only choice, lazy binding is not possible for them.

Upvotes: 2

ivaigult
ivaigult

Reputation: 6687

Try dlopen("/path/to/the/library", RTLD_LAZY);

Upvotes: 1

Related Questions