Search code examples
c++visual-studio-201764-bitcl

Use 64-bit compiler in Visual Studio


I use Visual Studio 2017. In a project (that I target as x64), I get error : C1060, compiler is out of heap space, and sadly learned there happen to exist a memory limitation for compilation.

When monitoring CL.exe, it indeed stop just before reaching 4GB. So it looks like CL.exe is by default a 32-bit application, as seen at https://learn.microsoft.com/en-us/cpp/build/how-to-enable-a-64-bit-visual-cpp-toolset-on-the-command-line

After reading this page, I installed "Universal Windows Platform workload" hoping to get access to a 64-bit version of CL.exe. But no change when compiling my project, and I can't see a single option in visual studio to choose compiler version.

I assume that there must exist a workaround to be able to use more than 4GB for a single compilation unit, but I couldn't find it for now. Any help would be much appreciated.

Edit : I hit limitation in Debug mode. Compilation is doing fine in Release mode. Which is suppose makes sense.


Solution

  • By default Visual Studio uses the 32-bit toolchain (i.e. the compiler is 32-bit and cross-compiles 64-bit executables). Visual Studio 2015 and 2017 include both 32-bit and 64-bit versions of all the compilers (x86, x64, arm, arm64).

    You can opt-in to using the 64-bit toolchain on a 64-bit system by two methods:

    1. Add a environment variable on your build machine (either system-wide or from a VS Developer Command Prompt).

    For example:

    set PreferredToolArchitecture=x64
    devenv
    
    1. You can edit your vcxproj files to do this as well with the <PreferredToolArchitecture>x64</PreferredToolArchitecture> element:

    For example:

    <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
        <ConfigurationType>Application</ConfigurationType>
        <UseDebugLibraries>true</UseDebugLibraries>
        <PlatformToolset>v141</PlatformToolset>
        <PreferredToolArchitecture>x64</PreferredToolArchitecture>
        <CharacterSet>Unicode</CharacterSet>
      </PropertyGroup>
    

    I use the second method in the UWP (C++/WinRT) versions of my Direct3D Game VS Templates, and I just noticed that I should add it to my UWP (C++/CX) and Win32 versions. The Xbox One XDK automatically does this in it's platform build rules as well.

    Note this question has been answered in the past: How to make Visual Studio use the native amd64 toolchain

    UPDATE: VS 2022 is 64-bit native, so it defaults to using the 64-bit tools. It also defaults to using 64-bit MSBuild where older products defaulted to 32-bit MSBuild.