1

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.

lhw
  • 538
  • 1
  • 6
  • 15
  • Isn't it duplicate of http://stackoverflow.com/q/480617/72178? Try to add `-rdynamic` when linking main executable. – ks1322 Sep 08 '15 at 14:45
  • The symbols are not available in the program or any linked library. I simply don't need them. – lhw Sep 08 '15 at 14:51

2 Answers2

2

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • This is indeed what I need. Sadly not yet available in my installed version of ld.gold. I will do the source modification for now and change it later to ld.gold. P.S. the linker line was just of the top of my head. – lhw Sep 14 '15 at 06:44
1

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

ivaigult
  • 6,198
  • 5
  • 38
  • 66
  • Should have mentioned it. The missing references are all variables which are not affected by RTLD_LAZY. But I don't think this would help with functions either. – lhw Sep 08 '15 at 14:12