visual-studio 有没有办法确定使用哪个版本的 Visual Studio 来编译静态库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1411854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to determine which version of Visual Studio was used to compile a static library?
提问by Bill Carey
I have a collection of static libraries (.lib) files one of which may have been built with a different version of Visual Studio. This is causing the code generation of a project that links against all of them to fail. Is there any way to determine which version of Visual Studio was used to compile a static library?
我有一组静态库 (.lib) 文件,其中一个文件可能是使用不同版本的 Visual Studio 构建的。这导致链接到所有这些的项目的代码生成失败。有什么方法可以确定使用哪个版本的 Visual Studio 来编译静态库?
采纳答案by James McNellis
For release libraries, it's unlikely that you could determine the version.
对于发布库,您不太可能确定版本。
For debug libraries, you can use dumpbin:
对于调试库,您可以使用dumpbin:
dumpbin /rawdata:1 library.lib
The assembly manifest should be at the beginning of the dump and will contain the version of the CRT the library requires along with the full path to the compiler used to build the library.
程序集清单应位于转储的开头,并将包含库所需的 CRT 版本以及用于构建库的编译器的完整路径。
For executables and DLLs you can get the linker version using dumpbin; it's under "OPTIONAL HEADER VALUES"
对于可执行文件和 DLL,您可以使用 dumpbin 获取链接器版本;它在“可选标题值”下
dumpbin /headers program.exe
Maybe someone else knows of a way to get the version for release libraries; I'm certainly interested too if they are.
也许其他人知道一种获取发布库版本的方法;如果他们是,我当然也很感兴趣。
回答by mheyman
I've always used something like (in a cygwin window):
我一直使用类似的东西(在 cygwin 窗口中):
strings -f *.lib | grep 'Visual Studio'
The compiler sticks the path of the compiler in the library on debug builds and the Visual Studio's compiler default location is under a path that includes the text 'Visual Studio'.
编译器在调试版本时将编译器的路径粘贴到库中,Visual Studio 的编译器默认位置位于包含文本“Visual Studio”的路径下。
So, like James McNellis' answer, this also works only for debug builds and is further restricted to builds that actually uses a compiler that sits in a directory with 'Visual Studio #' in the path.
因此,就像 James McNellis 的回答一样,这也仅适用于调试版本,并且进一步限于实际使用位于路径中带有“Visual Studio #”的目录中的编译器的版本。
I found this method years ago through a bit of serendipity and it has yet to fail.
几年前我偶然发现了这种方法,但它尚未失败。
This has the benefit that it is easy to remember if you are familiar with Unix command line tools.
如果您熟悉 Unix 命令行工具,这样做的好处是很容易记住。
回答by rustyx
If you have the corresponding .PDB files then you can see the version of the compiler from there with a tool like Pdb Inspector.
如果您有相应的 .PDB 文件,那么您可以使用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:
或者在十六进制查看器中打开 PDB 并搜索字符串“Microsoft (R) Optimizing Compiler”。版本将在该字符串之前的四个 2 字节十六进制值中,如下例所示:
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.
因此,版本为 HEX 13 00、00 00、6E 5D、00 00 或 19.0.23918.0。
回答by zevoid
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.
如果静态库是用 C++ 编写的,并且是用 MSVC 2010 或更新版本构建的,则编译器可能已将 FAILIFMISMATCH 指令放入目标文件中。
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.
我找不到微软关于 FAILIFMISMATCH 指令的官方文档,但链接器似乎使用它来检测 C++ 标准库版本之间的不兼容性。
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)
(或者如果您喜欢 cygwin 或 msys,则使用 mheyman 提到的方式)
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.
注意第一个指令:“_MSC_VER=NNNN”。根据我的观察,NNNN 始终与用于创建目标文件的编译器版本匹配。在我的例子中,xyz.lib 是用 MSVC 2015 update 3 创建的,它的 C++ 编译器版本是 19.00.24215,所以它把 /FAILIFMISMATCH:"_MSC_VER=1900" 放在目标文件中。
A detail mapping between Visual Studio version and Microsoft C/C++ Compiler version can be found at here.
可以在此处找到 Visual Studio 版本和 Microsoft C/C++ 编译器版本之间的详细映射。
回答by Jesse Chisholm
You didn't specify the language, but in C# the answer for knowing the OS and .NET version (in your code at runtime) is:
您没有指定语言,但在 C# 中,了解操作系统和 .NET 版本(在运行时的代码中)的答案是:
System.Version osVersion = System.Environment.OSVersion;
System.Version cliVersion = System.Environment.Version;
There would be an equivalent in Managed C++/CLI
在 Managed C++/CLI 中会有一个等价物
That won't tell you the verison of the compileror of the IDE, but will tell you the verison of the .NET runtimes. You may or may not need to know the OS version.
这不会告诉您编译器或IDE的版本,但会告诉您 .NET 运行时的版本。您可能需要也可能不需要知道操作系统版本。
-Jesse
-杰西

