Are Nested Static Library dependencies possible?
十一月 12, 2024 [C/C++] #obsidianAre Nested Static Library dependencies possible?
answer 1
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 (Static Link Library), 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 (Dynamic Link Library)
- 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
(原文配图在剪藏时保存失败——文件实为 HTML 错误页,无法恢复)
One SLL which was created from said .h File would require only the new SLL to be included for linking at compilation:
(原文配图在剪藏时保存失败——文件实为 HTML 错误页,无法恢复)
Hope this helps and that I have understood what you were asking.
answer 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.