2

Whenever you link static libraries into your Visual C++ project you have to make sure that the value for the runtime library of the project matches the value that was used to compile the library. If for example the library was compiled with the option for static runtimes and you try to compile the project with dynamic runtimes the linker will throw an error.

Obviously the linker has a way of determining whether a library was compiled with static or dynamic runtimes. I was wondering if there is a command line tool that takes the library and can directly tell me what runtimes were used during its creation?

Compuholic
  • 388
  • 4
  • 14

1 Answers1

6

dumpbin /directives is your friend:

C:\jm>type 08.cpp
int main() { }

C:\jm>cl /nologo /c /MT 08.cpp
08.cpp

C:\jm>dumpbin /nologo /directives 08.obj

Dump of file 08.obj

File Type: COFF OBJECT

   Linker Directives
   -----------------
   /DEFAULTLIB:LIBCMT
   /DEFAULTLIB:OLDNAMES

  Summary

          60 .debug$S
          2F .drectve
           7 .text$mn

C:\jm>

Note the /DEFAULTLIB:LIBCMT under "Linker Directives." This linker directive is injected into the object when you compile with /MT (static release CRT). The /MT, /MTd, /MD, and /MDd linker options all cause different runtime libraries to be dragged in. See the documentation for details.

A library is just a collection of objects, so you can use dumpbin /directives on a lib file to get the directives contained in each object in the library. Note that it's possible (though quite uncommon) for different objects in a single static library to be compiled with different runtime library options.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 3
    Looks great so far. But could it be that this only works for libs that were built in debug mode? For libs that were compiled in release mode the tool always comes back empty. – Compuholic Jun 25 '14 at 09:21
  • In the example here, `08.cpp` is compiled "in release mode" (`/MT` = static, release CRT). This was with Visual C++ 2013; I don't know what would happen with older versions, but I'd expect this to work. – James McNellis Jun 25 '14 at 17:24
  • 1
    So, what should we see in dumpbin output when running against a static .lib? And what should we see when running agains an dynamic .lib (pairing a .dll)? – KcFnMi Apr 07 '22 at 05:45
  • For dlls or lib files of dlls the tool always comes back empty for me. But with `/dependents` you can at least check dlls if they depend on the crt. Only for obj files the tool always lists the `/DEFAULTLIB:` directives. – ridilculous Jul 26 '23 at 15:55