C++ - Can you build one static library into anothe

2019-01-23 16:12发布

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

6条回答
祖国的老花朵
2楼-- · 2019-01-23 16:34

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-01-23 16:38

If you really want to do this, you can extract the .obj files from Lib2 and add them to Lib1.

See How to Extract .OBJ Routines from .LIB Files Using LIB.EXE -- I hope it is still relevant for VS2008.

查看更多
SAY GOODBYE
4楼-- · 2019-01-23 16:41

This is a fairly normal problem - you wouldn't normally attempt to include 'lib2' into 'lib1' but simply document that it's required to be linked against in order to work. There is nothing wrong with declaring the use of other libraries (apart from any licensing issues of course) so you are already doing the right thing.

查看更多
够拽才男人
5楼-- · 2019-01-23 16:43

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")
查看更多
祖国的老花朵
6楼-- · 2019-01-23 16:44

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).

查看更多
成全新的幸福
7楼-- · 2019-01-23 16:50

Instead of simply documenting your dependencies, use #pragma comment(lib, 'lib2name') in your code to make the linker pull in the other library automatically. Since you said you're using a standard library that comes with the SDK, this should eliminate all burden on the application.

查看更多
登录 后发表回答