36

我有一组静态库 (.lib) 文件,其中一个可能是使用不同版本的 Visual Studio 构建的。这导致链接所有项目的代码生成失败。有什么方法可以确定使用哪个版本的 Visual Studio 来编译静态库?

4

5 回答 5

30

对于发布库,您不太可能确定版本。

对于调试库,您可以使用dumpbin

dumpbin /rawdata:1 library.lib

程序集清单应位于转储的开头,并将包含库所需的 CRT 版本以及用于构建库的编译器的完整路径。

对于可执行文件和 DLL,您可以使用 dumpbin 获取链接器版本;它在“可选标题值”下

dumpbin /headers program.exe

也许其他人知道获取发布库版本的方法;如果他们是的话,我当然也很感兴趣。

于 2009-10-09T01:46:42.827 回答
11

我一直使用类似的东西(在 cygwin 窗口中):

strings -f *.lib | grep 'Visual Studio'

编译器在调试版本时将编译器的路径粘贴在库中,Visual Studio 的编译器默认位置位于包含文本“Visual Studio”的路径下。

因此,就像 James McNellis 的回答一样,这也仅适用于调试版本,并且进一步仅限于实际使用编译器的版本,该编译器位于路径中包含“Visual Studio # ”的目录中。

几年前,我偶然发现了这种方法,但它还没有失败。

This has the benefit that it is easy to remember if you are familiar with Unix command line tools.

于 2012-06-06T19:10:45.757 回答
11

If you have the corresponding .PDB files then you can see the version of the compiler from there with a tool like Pdb Inspector.

Or open the PDB in a hex viewer and search for the string "Microsoft (R) Optimizing Compiler". The version will be in four 2-byte hex values just before that string, like in this example:

000000A060: .. .. .. .. .. .. . ...  .. .. .. .. .. .. 13 00                ..
000000A070: 00 00 6E 5D 00 00 4D 69  63 72 6F 73 6F 66 74 20  ......Microsoft
000000A080: 28 52 29 20 4F 70 74 69  6D 69 7A 69 6E 67 20 43  (R) Optimizing C
000000A090: 6F 6D 70 69 6C 65 72 00  .. .. .. .. .. .. .. ..  ompiler ........

The version is thus HEX 13 00, 00 00, 6E 5D, 00 00, or 19.0.23918.0.

于 2016-05-19T08:45:36.110 回答
0

If the static library was written in C++, and was built with MSVC 2010 or newer version, a FAILIFMISMATCH directive may have been put by compiler into the object files.

I cannot found the official document from Microsoft about the FAILIFMISMATCH directive, but it seems to be used by linker to detect incompatibilities between C++ standard library versions.

You can use the following command to print out those directives from a static library:

find "FAILIFMISMATCH" xyz.lib

(or use the way that mheyman has mentioned if you favor in cygwin or msys)

The result may be similar to this:

0@   /FAILIFMISMATCH:"_MSC_VER=1900" /FAILIFMISMATCH:"_ITERATOR_DEBUG_LEVEL=0" /FAILIFMISMATCH:"RuntimeLibrary=MD_DynamicRelease" /DEFAULTLIB:"msvcprt" /FAILIFMISMATCH:"_CRT_STDIO_ISO_WIDE_SPECIFIERS=0" /DEFAULTLIB:"uuid.lib" /DEFAULTLIB:"uuid.lib" /DEFAULTLIB:"MSVCRT" /DEFAULTLIB:"OLDNAMES"

Note the first directive: "_MSC_VER=NNNN". In my observation, the NNNN is always match to the compiler version used to create the object file. In my case, the xyz.lib was create with MSVC 2015 update 3, its C++ compiler version is 19.00.24215, so it put /FAILIFMISMATCH:"_MSC_VER=1900" in object file.

A detail mapping between Visual Studio version and Microsoft C/C++ Compiler version can be found at here.

于 2017-09-05T08:23:03.760 回答
-4

You didn't specify the language, but in C# the answer for knowing the OS and .NET version (in your code at runtime) is:

System.Version osVersion = System.Environment.OSVersion;
System.Version cliVersion = System.Environment.Version;

There would be an equivalent in Managed C++/CLI

That won't tell you the verison of the compiler or of the IDE, but will tell you the verison of the .NET runtimes. You may or may not need to know the OS version.

-Jesse

于 2012-06-25T20:40:44.097 回答