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

烂漫一生 提交于 2019-11-29 01:56:18
Lou Franco

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

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

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.

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!