C++ 如何让 Visual Studio 使用原生 amd64 工具链

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

How to make Visual Studio use the native amd64 toolchain

c++visual-studiocompilation64-bit

提问by Ky Waegel

How can I get Visual Studio 2012 to use the native amd64 toolchain, rather than the default x86_amd64 cross-compiler?

如何让 Visual Studio 2012 使用本机 amd64 工具链,而不是默认的 x86_amd64 交叉编译器?

I am building a large library that causes the linker to run out of memory when doing whole program optimization and link-time code generation.

我正在构建一个大型库,导致链接器在执行整个程序优化和链接时代码生成时内存不足。

I found two older posts (hereand here) asking this same question, but no answers yet. Microsoft provides documentation on how to specify the toolchain on the command line, but not in the IDE.

我发现两个较旧的帖子(这里这里)提出了同样的问题,但还没有答案。Microsoft 提供了有关如何在命令行中而不是在 IDE 中指定工具链的文档。

回答by Denis Gurchenkov

You need to set the environment variable "_IsNativeEnvironment" to "true" prior to starting Visual Studio 2012 IDE:

在启动 Visual Studio 2012 IDE 之前,您需要将环境变量“_IsNativeEnvironment”设置为“true”:

set _IsNativeEnvironment=true
start "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe" your_solution.sln

For Visual Studio 2013, the name of the environment variable is different:

对于 Visual Studio 2013,环境变量的名称不同:

set PreferredToolArchitecture=x64
sbm start "C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe" your_solution.sln

Beware that this technique does not work if the version of the IDE does not match the version of the toolchain. That is, if you use VS2013 IDE configured to run VS2012 compiler, you are out of luck. But such combination is uncommon.

请注意,如果 IDE 的版本与工具链的版本不匹配,则此技术不起作用。也就是说,如果您使用配置为运行 VS2012 编译器的 VS2013 IDE,那您就不走运了。但这样的组合并不常见。

Here are some links for further information:

以下是一些链接以获取更多信息:

difference between VS12 and VS13

VS12和VS13的区别

how to embed PreferredToolArchitecture into the project in VS13

如何在 VS13 中将 PreferredToolArchitecture 嵌入到项目中

回答by the_mandrill

There is another method of forcing use of the 64-bit linker on a per-project basis for Visual Studio 2013. Edit your .vcxproj file and insert the following after the <Import...Microsoft.Cpp.Defaultsline:

还有另一种在 Visual Studio 2013 的每个项目的基础上强制使用 64 位链接器的方法。编辑您的 .vcxproj 文件并在该<Import...Microsoft.Cpp.Defaults行后插入以下内容:

  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
  <PropertyGroup>
    <PreferredToolArchitecture>x64</PreferredToolArchitecture>
  </PropertyGroup>

回答by Ross Bencina

If your goal is to use the nativeenvironment rather than specifically amd64_x86, you can set the UseNativeEnvironmentproperty in your project file:

如果您的目标是使用本环境而不是专门使用amd64_x86,则可以UseNativeEnvironment在项目文件中设置该属性:

<PropertyGroup>
  <UseNativeEnvironment>true</UseNativeEnvironment>
</PropertyGroup>

(I have successfully added it to the "Globals" PropertyGroup.)

(我已成功将其添加到“Globals”PropertyGroup。)

You can check which toolchain is being used by adding the /Bvcompiler option. Example output is below. Notice that the toolchain directory appears after bin\(amd64_x86in this case).

您可以通过添加/Bv编译器选项来检查正在使用哪个工具链。示例输出如下。请注意,工具链目录出现在bin\(amd64_x86在本例中) 之后。

2>ClCompile:
2>  Compiler Passes:
2>   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\CL.exe:        Version 18.00.31101.0
2>   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\c1.dll:        Version 18.00.31101.0
2>   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\c1xx.dll:      Version 18.00.31101.0
2>   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\c2.dll:        Version 18.00.31101.0
2>   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x86\link.exe:      Version 12.00.31101.0
2>   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64\mspdb120.dll:      Version 12.00.31101.0
2>   C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64_x8633\clui.dll: Version 18.00.31101.0

回答by Don Pedro

I know this is a rather old post, but it's still relevant for VS 2017. Here you also have the "PreferredToolArchitecture" environment variable and a "build in" setting in the IDE is not readily available.

我知道这是一个相当老的帖子,但它仍然与 VS 2017 相关。在这里你还有“PreferredToolArchitecture”环境变量,IDE 中的“内置”设置并不容易获得。

However you can integrate that easily on a project by project base so that you can always chose what tool architecture to use. Maybe this is helpful for some. Do this:

但是,您可以按项目轻松地将其集成到一个项目中,以便您始终可以选择要使用的工具架构。也许这对某些人有帮助。做这个:

  • Go to Property Manager and create a new property sheet, e. g. named "x64 Toolchain.props" so you know what it does. With a separate property sheet you can then switch the tool architecture as appropriate by including or not including the sheet into a project.
  • Open the properties of that new sheet, navigate to "Common Properties\User Macros" and click "Add Macro".
  • In the dialog you set the name to "PreferredToolArchitecture", the value to "x64" and enable the checkbox "Set this macro as an environment variable in the build environment".
  • Optionally navigate to "Common Properties\C/C++\Command Line" and add "/Bv" under "Additional Options". This will make the compiler output the tools it uses including its path and version number, might be helpful for checking if really the desired architecture is being used. It will place entries in the log output window like so:

    Compiler Passes:
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\CL.exe: Version 19.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\c1.dll: Version 19.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\c1xx.dll: Version 19.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\c2.dll: Version 19.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\link.exe: Version 14.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x86\mspdb140.dll: Version 14.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\1033\clui.dll: Version 19.15.26730.0

  • Now for all projects that should be build with the x64 tool architecture include the new property sheet into the project in property manager. And for those that should not simply don't include it. That's it.
  • 转到属性管理器并创建一个新的属性表,例如命名为“x64 Toolchain.props”,以便您了解它的作用。使用单独的属性表,您可以通过在项目中包含或不包含该表来根据需要切换工具架构。
  • 打开该新工作表的属性,导航到“通用属性\用户宏”,然后单击“添加宏”。
  • 在对话框中,将名称设置为“PreferredToolArchitecture”,将值设置为“x64”并启用复选框“将此宏设置为构建环境中的环境变量”。
  • (可选)导航到“Common Properties\C/C++\Command Line”并在“Additional Options”下添加“/Bv”。这将使编译器输出它使用的工具,包括其路径和版本号,可能有助于检查是否确实使用了所需的架构。它将在日志输出窗口中放置条目,如下所示:

    编译器通过:
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\CL.exe:版本 19.15.26730.0
    C:\Program Files( x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\c1.dll:版本 19.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\ Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\c1xx.dll:版本 19.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15 .26726\bin\HostX86\x64\c2.dll:版本 19.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\ link.exe:版本 14.15.26730.0
    C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x86\mspdb140.dll:版本 14.15.26730.0
    C:\Program Files (x86)\ Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\bin\HostX86\x64\1033\clui.dll:版本 19.15.26730.0

  • 现在,对于应该使用 x64 工具架构构建的所有项目,将新的属性表包含到属性管理器中的项目中。对于那些不应该简单地不包括它的人。就是这样。

HTH

HTH

Edit: It seems unfortunately this is not reliable! See below comments. I would really appreciate if MS would wire this setting to some GUI element and make it work consinstently...

编辑:不幸的是,这似乎不可靠!请参阅下面的评论。如果 MS 将此设置连接到某个 GUI 元素并使其始终如一地工作,我将不胜感激...

回答by Henry Garcia

I have a similar problem using Visual Studio 2010 on XP 64 SP2. If I set the VC++ executable directory to the amd64 bin (the native x64 folder) as the first in the search path, then I received the TRK0002 error … Invalid handle value.

我在 XP 64 SP2 上使用 Visual Studio 2010 时遇到了类似的问题。如果我将 VC++ 可执行目录设置为 amd64 bin(本机 x64 文件夹)作为搜索路径中的第一个,那么我收到了 TRK0002 错误……句柄值无效。

But if I set _IsNativeEnvironment=true in a Visual Studio 2010 command prompt and start the ide from the command line as posted before, then the error goes away. Apparently, the 32-bit GUI IDE Environment is receiving info from a 64 bit process and is expecting info from a 32-bit process such as x86\cl.exe or x86_64\cl.exe.

但是,如果我在 Visual Studio 2010 命令提示符中设置 _IsNativeEnvironment=true 并从之前发布的命令行启动 ide,那么错误就会消失。显然,32 位 GUI IDE 环境正在接收来自 64 位进程的信息,并期待来自 32 位进程(例如 x86\cl.exe 或 x86_64\cl.exe)的信息。

In a scenario where you want to compile an IA64 bit executable, and you use the x86_ia64\cl.exe compiler. Since you are using a 32-bit cross compiler and have the _IsNativeEnvironment variable set to true, this must upset the IDE when posting messages in it's window consoles. Set the _IsNativeEnvironment=false, if you have previously set it to true.

在您想要编译 IA64 位可执行文件并使用 x86_ia64\cl.exe 编译器的情况下。由于您使用的是 32 位交叉编译器并且将 _IsNativeEnvironment 变量设置为 true,因此在其窗口控制台中发布消息时,这肯定会扰乱 IDE。设置 _IsNativeEnvironment=false(如果您之前已将其设置为 true)。

The IDE should pick up that a native compiler was being used on a native 64-bit machine and should have automatically set this variable to the appropriate value when the native compiler was selected from the IDE. A simple fix has never been applied to patch this problem. Solution. Do it yourself from the prompt, or buy the latest IDE from Microsoft to fix the problem.

IDE 应该检测到在本机 64 位机器上使用本机编译器,并且在从 IDE 选择本机编译器时应该自动将此变量设置为适当的值。从未应用简单的修复来修补此问题。解决方案。根据提示自行完成,或从 Microsoft 购买最新的 IDE 来解决问题。

So, the real wizards at Microsoft are the developers who work mainly from the command line. And the other developers, who wear the pointy hats and sit in the corner, must have been hired from Apple and were more concerned about appearance than function.

因此,Microsoft 真正的向导是主要从命令行工作的开发人员。而其他戴着尖顶帽子坐在角落里的开发人员肯定是从苹果公司雇佣来的,他们更关心外观而不是功能。

The whole purpose of an IDE is to make coding simple, not more complicated than using a text editor and a Makefile from the command line.

IDE 的全部目的是使编码变得简单,而不是比从命令行使用文本编辑器和 Makefile 更复杂。