Hannah S.
Hannah S.

Reputation: 3320

How to optionally depend on a shared object with gcc?

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

I have the following situation:

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:

My questions:

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

Additional Info:

Upvotes: 6

Views: 6359

Answers (2)

Dmitry Yudakov
Dmitry Yudakov

Reputation: 15734

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.

Upvotes: 6

Douglas Leeder
Douglas Leeder

Reputation: 53310

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.

Upvotes: 7

Related Questions