Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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
like image 337
Hanno S. Avatar asked Aug 05 '10 11:08

Hanno S.


People also ask

What are shared object dependencies?

During the link-edit of a dynamic executable, one or more shared objects are explicitly referenced. These objects are recorded as dependencies within the dynamic executable. The runtime linker uses this dependency information to locate, and load, the associated objects.

What does shared do in GCC?

Produce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options used for compilation (-fpic, -fPIC, or model suboptions) when you specify this linker option.

How are shared libraries loaded?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

What is dyn shared object?

A dynamic shared object (DSO) is an object file that is meant to be used simultaneously (or shared) by multiple applications (a. out files) while they are executing.


2 Answers

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.

like image 54
Douglas Leeder Avatar answered Sep 22 '22 18:09

Douglas Leeder


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.

like image 33
Dmitry Yudakov Avatar answered Sep 22 '22 18:09

Dmitry Yudakov