visual-studio 使用VS2008为64位windows平台编译C++静态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1393704/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:58:53  来源:igfitidea点击:

compiling C++ static library for 64 bit windows platform using VS2008

cvisual-studio64-bit

提问by

ith Visual Studio 2008, If the configuration type is a static library, I dont see a linker option in project properties. I need to specify /MACHINE:x64 option for a static library. I tried to specify this in command line option in Librarian. Only then I could build the static library. If I dont specify /MACHINE compiling the static lib fails with LNK1112: module machine type 'X86' conflicts with target machine type 'x64' ( even if I set the platform to X64 for my solution).

在 Visual Studio 2008 中,如果配置类型是静态库,我在项目属性中看不到链接器选项。我需要为静态库指定 /MACHINE:x64 选项。我试图在图书馆员的命令行选项中指定这一点。只有这样我才能构建静态库。如果我不指定 /MACHINE 编译静态库失败并显示 LNK1112:模块机器类型“X86”与目标机器类型“x64”冲突(即使我将平台设置为 X64 作为我的解决方案)。

With /MACHINE:X64 specified as command line through Project-Properties-Librarian, the static library was built but other project (of configuration type : DLL) in the same solution has a dependency on the static lib, when the DLL is built and tries to use one of the functions in the lib I again get the same error:

通过 Project-Properties-Librarian 将 /MACHINE:X64 指定为命令行,构建了静态库,但同一解决方案中的其他项目(配置类型:DLL)依赖于静态库,当构建 DLL 并尝试时要使用 lib 中的函数之一,我再次遇到相同的错误:

fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'

致命错误 LNK1112:模块机器类型“X86”与目标机器类型“x64”冲突

Please suggest, how do I build a 64 bit static library

请建议,我如何构建一个 64 位静态库

回答by Stuart Lange

For what it's worth, I've come across this exact same issue.

对于它的价值,我遇到了这个完全相同的问题。

I have a project that compiles a static library, and creating an "x64" configuration did NOT get it actually targeting x64. I had to explicitly add "/MACHINE:X64" as an "additional option" under "Librarian -> Command Line" in the project's property pages, just as you did.

我有一个编译静态库的项目,但创建“x64”配置并没有让它真正针对 x64。我必须像您一样,在项目属性页的“图书馆员-> 命令行”下明确添加“/MACHINE:X64”作为“附加选项”。

I would expect visual studio to expose this setting as a first-class property in the property pages, as it does for dynamic libraries (under "Linker -> Advanced -> Target Machine"). Perhaps I'm missing something.

我希望 Visual Studio 将此设置公开为属性页中的一流属性,就像它对动态库所做的那样(在“链接器 -> 高级 -> 目标机器”下)。也许我错过了一些东西。

回答by the_mandrill

As mentioned by Timbo, you need to ensure that you have an x64 configuration that you're building. However, there are a couple of other gotchas to be aware of:

正如 Timbo 所提到的,您需要确保您拥有正在构建的 x64 配置。但是,还有一些其他问题需要注意:

  • Do a total clean of your build directory first in case you still have some 32 bit object files lying around that are causing confusion
  • Check the intermediate and output directories for your project. If the destination is Debug\Mylib.libthen you're going to run into problems because the same name is being used for the 32 and 64 bit libraries. I prefer to select all configurations and all platforms and then rename them all to something standard like ..\build\$(ProjectName)\$(ConfigurationName).$(PlatformName)
  • Check in the configuration manager that when you are building the 64-bit configuration of the solution that it is configured to build the 64-bit configurations of the projects (this doesn't always happen by default)
  • The error: module machine type 'X86' conflicts with target machine type 'x64'means that the object file has been built as 32-bit but the link setting of the project is set with the flag /machine:x64. So this suggests that the project configuration is 32-bit.
  • If in doubt about what you've created, pick one of the object files and type this at a command prompt:

    dumpbin /headers myfile.obj | findstr machine

  • 首先彻底清理您的构建目录,以防您周围仍有一些 32 位目标文件会引起混淆
  • 检查项目的中间目录和输出目录。如果目标是,Debug\Mylib.lib那么您将遇到问题,因为 32 位和 64 位库使用相同的名称。我更喜欢选择所有配置和所有平台,然后将它们全部重命名为标准名称,例如..\build\$(ProjectName)\$(ConfigurationName).$(PlatformName)
  • 检查配置管理器,当您构建解决方案的 64 位配置时,它被配置为构建项目的 64 位配置(默认情况下并不总是发生这种情况)
  • 错误:module machine type 'X86' conflicts with target machine type 'x64'表示目标文件已构建为 32 位,但项目的链接设置是使用标志设置的/machine:x64。所以这表明项目配置是 32 位的。
  • 如果对您创建的内容有疑问,请选择目标文件之一并在命令提示符下键入:

    dumpbin /headers myfile.obj | findstr machine

This will show you the architecture you've actually built for.

这将向您展示您实际构建的架构。

回答by Timbo

Did you try to add a new project configuration (x64) to the existing project?

您是否尝试向现有项目添加新的项目配置 (x64)?

You should usually not have to edit the project properties at all to build in 64bit. You just have to add the configurations and make sure the solution configuration is correct (64 bit solution configuration contains 64bit project configurations).

您通常根本不必编辑项目属性即可构建 64 位。您只需要添加配置并确保解决方案配置正确(64 位解决方案配置包含 64 位项目配置)。

You can check this by opening Build->Configuration Manager. My Visual Studio sometimes messes with these settings and makes the project uncompilable, so checking it again might help.

您可以通过打开Build->Configuration Manager来检查这一点。我的 Visual Studio 有时会弄乱这些设置并使项目无法编译,因此再次检查它可能会有所帮助。