Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am working in QT .

  1. Can a static library depend on another static library?(Static Lib is made by linking another static lib)
  2. If yes, is it possible that after linking to lib2, the generated lib(lib1) would not contain all the codes of lib2?

In my Qt Project I am using a static library, which depends on multiple libraries. I had to add all the libraries (with all their headers in my project), although I need only one lib (and one .h of that class) in my code.

Please explain the scenario.

share|improve this question

2 Answers 2

When you declare something you just promise that during linking it will be available and uniquely identified. Static libs are nothing more than containers for definitions in a format which is outside the standard. So, this has the following implications:

1 ) I don't quite get what you're asking here, but one static library can very much depend on another static library. Either you promise that the definitions of that library be available during the final linking stage of your executable ( by providing it as link dependency ). Or you "merge" it during linking of the static lib.

2 ) The answer to this then becomes that the only thing you need to do is simply to not link in lib2 during linking of lib1.

Overall the best approach in my experience is to only merge libs which are private, the reason being that otherwise clients will run into multiply defined symbols violations when other parts of their project shares dependencies with you.

share|improve this answer
    
It means, A static lib which depends on another static lib ,but contains the linking information of that lib IS POSSIBLE.That means when I need to include the first lib in my project, I am bound to include the second also. Bcoz, the first lib does not contain whole codes of second lib.Am I right? –  sk patra Sep 30 '13 at 11:49

My quick two bits on this: 10 (sorry terrible joke).

To expand slightly, here goes:

1- A static library can depend on another static library, nothing or even a dynamic library: in the first two instances all the code for the new static library would be incorporated in the new sll, however, the 3rd option, depending on evironment could lead to the sll having one of the following outcomes,

  • it would contain static versions of the code required by it gathered from the DLL
  • or, much more horribly, you would have a static library that when included in a project would cause the requirement of shipping with a DLL, breaking the whole point of static linking.

So in short yep, you can link a static library to a static library

2- if lib1 and lib2 are both static, and lib1 has been linked with lib2, then all the functionality of lib2 would be available within lib1 without the need to include lib2 as lib2 would be statically linked within lib1 (think russian dolls, with lib2 being the smaller).

In the scenario you describe there must be discrete functionality available in each of the files you are having to include, which is not statically linked, hence the need for multiple files, otherwise you would have a single file to include and you would be fine.

Take the following: One .h file with references to 6 statically linked libraries: you would need to include 7 files in your project to get full functionality

inclusion example 1

One sll which was created from said .h File would require only the new sll to be included for linking at compilation:

inclusion example 2

Hope this helps and that I have understood what you were asking.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.