1

Basic question: Can one specify, when building a static library (and ONLY when building the static library), that the static library needs to link with a shared object?

I'm working on a project that looks like this:

  • StaticLib
  • Application1 - links with StaticLib
  • SharedObject - links with StaticLib
  • Application2 - links with SharedObject

I only own StaticLib.

I'd like to add a dependency on a 3rd party SharedObject to the StaticLib. I know that the 3rd party SO exists on all the machines that my software is going to run on, which is good because I'm also restricted by licensing from distributing that 3rd party SO in anyway.

Just playing around, I can get it to work so long as I tell the linker at every application level that I need to link with the 3rd party SO. The problem is twofold: I don't own the build process for those applications AND I'm actually simplifying and there are a lot of applications and SOs that use my static library, some of which are completely outside of my org and I don't know what they are.

As I said, I'm restricted by licensing from distributing the 3rd party SO, so the solutions I've seen that talk about how to fold the SO into a static library won't work for me; that would be distributing the 3rd party SO and I'm legally blocked from doing that.

Is there any way to make this work?

As a follow up, MOST of the uses of my static library will be via SharedObject. If there's a way to make this work such that I must modify the build process for the handful of applications that directly use StaticLibrary and and modify the build of SharedObject, but not have to modify the build of applications that use SharedObject, that'd be workable.

1 Answer 1

0

A static library as input to the GNU linker is a simple archive of object files from which the linker will copy and statically link into the output image exactly those object files, if any, that provide the linkage with definitions for symbols that are already referred to in the linkage but so far lack definitions.

A static library contributes nothing whatsoever to the linkage but the 0 or more object files that are extracted from it.

So your question boils down to: is there a way in which gcc can compile an object file that when input to a linkage will instruct the linker to find a specified shared library and input it to the linkage?

There isn't.

High-level build system generators (autotools, cmake, meson, buck, etc.) will enable you to specify somehow that when a library A is in the linkage of a build target then so will be libraries B, C ..., in that order. The details vary. You might distribute such a build system.

Sign up to request clarification or add additional context in comments.

3 Comments

That's what I expected regarding the static library, but what about the shared object? I know far less about them. Are they also just a bunch of object files bundled together and then "linked to" at application load time? My background is more from the Windows world, and as an example, making DLLOne use DLLTwo doesn't require that the build process for APPAlpha that uses DLLOne know anything about DLLTwo. If some popular open source SharedObjectOne started using SharedObjectTwo, would all of the apps using the SharedObjectOhe suddenly have broken builds?
@TimWilliams Welcome to SO. No, a shared library (a.k.a DSO - dynamic shared object) is output by the linker in much the same way as a program and the differences between a program and a DSO are small enough that the same binary can to be both at once. The other questions you raise can't be answered within these 500 bytes. Post a new question if you want. Also consider What should I do when someone answers my question?
Thanks for the help. And to answer my own question, yes, if you link the 3rd party SO in with SharedObject above, then Application2 doesn't need to know about it. And I can't upvote, I don't have enough reputation. But you did answer my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.