Static library with dependencies

2019-02-22 00:15发布

e.exe is linked against my custom static library, c.lib, which uses Win32 API defined in w.dll. w.dll is located in C:\Windows\System32 and its import library is w.lib, located in Windows SDK directory. Shell w.lib be listed as Additional Dependency in c.lib or e.exe project? (e.exe builds successfully in both cases.) What is the best practice and why? I guess e.exe should not know about w.lib.

c.lib is intended to be shared among a group of developers only (not to be shipped to customers).

TEST: I used VS2008 and dumpbin utility to test both cases and here are results:

  • Case 1: w.lib added as Additional Dependency in c.lib project.

dumpbin /archivemembers c.lib output lists both offsets in w.dll and .obj files from c.lib project as Archive members.

  • Case 2: w.lib not added as Additional Dependency in c.lib but in e.exe project:

This time, dumpbin output contains only .obj files of c.lib and the size of c.lib is smaller than in Case 1

(c.lib was added as Additional Dependency in w.exe project in both cases.)

NOTE: I used w.lib and w.dll here as fictional, generic names for Windows libraries but they could be e.g. Userenv.lib and Userenv.dll or Version.lib and Version.dll...

2条回答
Root(大扎)
2楼-- · 2019-02-22 00:47

I think you're misunderstanding what creating an archive and an import archive does.

Creating an archive, as you've rightly surmised in comments, creates a unified file containing compiled .objs. Now, this can contain any code you like, including but not limited to dynamic calls to libraries. An import library is a library that contains an obj that exclusively makes such calls, the idea being that by importing it, your exe can find the appropriate symbols (they must be in the executable you create).

The process of creating c.lib from w.lib simply extracts w.lib's objects and appends them to the collection of objects in c.lib. In effect, c.lib becomes an import library + code.

Do I think you should do this? Not really - it might lead to confusion as to what e.exe depends on; I think you should explicitly make this visible rather than trying to hide it. That said, that's a recommendation only, not a rule.

查看更多
贼婆χ
3楼-- · 2019-02-22 00:51

Libraries are not linked, so any project that uses a .lib also needs its dependencies.

Basically the .lib are "copied" to your exe during linking.

If you want to avoid your users to explicity link againts w.lib, transforms c.lib in a dll, dlls are linked and you do not need their dependencies during build.

查看更多
登录 后发表回答