views:

58

answers:

2

First, I don't know if there is a solution to my problem at all.

I have the following situation:

  • I have developed a framework library that depends on several other libraries for specific hardware access, etc.
  • Until now this framework library was only statically linked against.
  • For the executable that uses the framework library only the dependencies of code that is actually used by the executable have to be linked. (If I don't access a specific hardware at all I don't have to depend on its associated libraries.)

Now I need to also make a shared object of the framework library. Also the dependencies are available as shared libraries, so there is no need for any static linking.

The problem I have now:

  • When building an application that links dynamically to the framework library I have to either link all dependencies dynamically to the framework library or the application. (Otherwise I get undefined references complaints from ld)

My questions:

  • Is there any way to ignore certain shared object dependencies if I know that my application will not use any code of the framework library that depends on this shared object?

  • Is there any way to do this without or with minimal code changes? (linker / compiler switches)

I also need the static linking as described in the original situation to still work.

Additional Info:

  • Operating system: Linux (Debian Lenny)
  • Compiler: gcc-4.3
+7  A: 

You can, but you basically have to do all of the dynamic library handling yourself. i.e. dlopen the library, and then look up the symbols you need directly with dlsym.

It will make your code more complicated, how much depends on the interface you've got into the libraries.

Douglas Leeder
This is a good hint - but in my case this would introduce too much code changes.
Hanno Stock
+3  A: 

From man ld

--as-needed
--no-as-needed

This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option. Normally, the linker will add a DT_NEEDED tag for each dynamic library mentioned on the command line, regardless of whether the library is actually needed. --as-needed causes a DT_NEEDED tag to only be emitted for a library that satisfies a symbol reference from regular objects which is undefined at the point that the library was linked, or, if the library is not found in the DT_NEEDED lists of other libraries linked up to that point, a reference from another dynamic library. --no-as-needed restores the default behaviour.

I haven't used it myself but sounds like what you're looking for.

g++ -o your_app -Wl,--as-needed -lframework -la -lb -lc -Wl,--no-as-needed

Edit (suggested by Hanno)

--warn-unresolved-symbols

If the linker is going to report an unresolved symbol (see the option --unresolved-symbols) it will normally generate an error. This option makes it generate a warning instead.

Dmitry Yudakov
marking this as the answer as it comes closest to the solution I wish for. Still this needs all the libraries available at the initial link of the application but they do not have to be distributed with the application.
Hanno Stock
Update: Using -Wl,--warn-unresolved-symbols one can make circumvent needing all the libraries at application link time.In this case it is up to the developer to check if the unresolved symbols might be needed by the application.
Hanno Stock