Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Can you build one static library into another?

I ran into a strange problem with a Visual Studio 2008 project I was working with recently.

I am trying to compile a new static library that uses functions from another static library. (Let's say Lib1 is my static library project, and Lib2 is the lib file that Lib1 depends on).

I am able to build lib1 without issue; It includes the header files for lib2 and calls its functions, and there are no problems.

The problem is when I build a separate test project that has Lib1 as a dependency; it won't build and I get linker errors. The unresolved externals are the functions I am trying to call within Lib1 that are from Lib2.

This is all fixed when I include Lib2 in my test project as well.

This all makes sense to me of course; I can test that Lib2 is not being built into Lib1..

My question is: is there a way to do this? I would ideally like to be able to deploy Lib1 as a standalone lib without requiring Lib2. (Lib2 is actually just a Lib from the Windows Platform SDK, so it's not really a big deal...)

Is this not allowed because it would allow people to "hide" third party libraries in their own, or something?

What would be a professional approach to this problem?

Thanks!

--R

like image 370
8bitcartridge Avatar asked Mar 26 '11 20:03

8bitcartridge


People also ask

Can a static library depend on another static library?

Static Library Chains This is because static libraries don't link to other static libraries: All of the executable code only gets embedded in the executable. Technically, then, no static library depends on any other static library.

Are static libraries linked?

A static library is a programming concept in which shared libraries with special functionalities, classes or resources are linked to external applications or components, facilitating the creation of stand-alone and executable files.

What happens when you link a static library?

Static Linking and Static Libraries is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory.


3 Answers

I would not advise using a librarian to take Windows' library contents into your own library -- it's likely that that's against the license.

I see two possibilities

  1. Documenting the dependency
  2. Using a #pragma in your .h file that requests the .lib to be linked against. If VS can find it, it's the same as including it on your link line.

http://msdn.microsoft.com/en-us/library/7f0aews7(VS.80).aspx

 #pragma comment(lib, "libname.lib")
like image 168
Lou Franco Avatar answered Oct 17 '22 10:10

Lou Franco


You need to use a tool called a librarian to do this. A librarian allows you to create and modify library (.lib) files. In visual studio check under the Librarian section of your project properties. A command line version also comes with visual studio (lib.exe).

like image 3
Ferruccio Avatar answered Oct 17 '22 11:10

Ferruccio


Just document the dependencies of your lib.

As long as the library you depend on is available to anyone that could use your library, this is the preferred solution. Especially considering that the library user could also depend on this platform SDK lib - if you had it embedded then he'd get funny linker errors with multiply defined symbols.

like image 2
Erik Avatar answered Oct 17 '22 11:10

Erik