【问题标题】:VS2017 developer command prompt only builds to x86 - need x64VS2017 开发人员命令提示符仅构建到 x86 - 需要 x64
【发布时间】:2021-02-23 14:43:34
【问题描述】:

我正在尝试使用 VS2017 开发人员命令提示符使用 cmake 构建一些 C++ 库。我需要为 Release x64 设置构建它们,但是,命令提示符似乎只将它们构建到 x86。

首先,我运行这个命令(注意构建类型Release64):

cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release64 -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- The C compiler identification is MSVC 19.16.27039.0
-- The CXX compiler identification is MSVC 19.16.27039.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x86/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
--
-- 3.13.0.0
-- Looking for pthread.h
-- Looking for pthread.h - not found
-- Found Threads: TRUE
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
-- Configuring done
-- Generating done
-- Build files have been written to: C:/test_game_server/protobuf/cmake/build/release64

然后我运行nmake,但我看到了这个错误:

...
[ 49%] Linking CXX static library gmock_main.lib
[ 49%] Built target gmock_main
[ 50%] Generating C:/test_game_server/protobuf/src/google/protobuf/any_test.pb.cc
google/protobuf/any_test.pb.cc: while trying to create directory C:/test_game_server/protobuf/src/google: No error
NMAKE : fatal error U1077: '.\protoc.exe' : return code '0x1'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.

即使我使用Vcvarsall.batset a 64-bit hosted build architecture 后仍然出现此错误:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.22
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

值得注意的是,当我使用选项Release而不是Release64编译项目时没有错误。

我查找了 x64 native or cross-tool developer command prompts,但我没有任何与 VS2017 相关的内容(我确实有一个用于 VS2010 但 C++ 版本已过时)。在我的标准 Windows 命令提示符下编译不是一个选项。

如何强制此项目编译为 x64 而不是 x86?

【问题讨论】:

    标签: c++ visual-studio cmake visual-studio-2017


    【解决方案1】:

    您没有正确使用 CMAKE_BUILD_TYPE。

    https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type

    除非您定义了除默认配置(Debug、Release、RelWithDebInfo、MinSizeRel)之外的配置,否则您所做的一切都是没有意义的。

    配置与您正在编译的架构无关。

    您正在编译的架构与工具链和编译器选项有关。

    ========================================

    第二次

    NMake Makefile 非常慢。请改用 Ninja 或 Visual Studio。

    ========================================

    第三

    以下是使用 Visual Studio 构建 x64/x86 的说明(我使用的是 2019,但没关系)

    cmake -G "Visual Studio 16 2019" -A Win32 -S path_to_source -B "build32" cmake --build build32 --config 发布

    cmake -G "Visual Studio 16 2019" -A x64 -S path_to_source -B "build64" cmake --build build64 --config 发布

    ========================================

    第四个

    如果您想知道如何为 ninja/nmake-builds 等单一配置生成器配置/构建,请提出另一个问题。因为这是一个加载的问题。

    【讨论】:

      【解决方案2】:

      如果您不需要使用 NMake,您可以使用 Visual Studio 的构建系统,首先生成 64 位(注意 Win64 部分):

      cmake -G "Visual Studio 15 2017 Win64"  -DCMAKE_BUILD_TYPE=Release64 -Dprotobuf_BUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=../../../../install ../..
      

      然后通过 cmake 的build 命令构建:

      cmake --build <directory> --config Release64
      

      然后,CMake 将生成并执行在命令行上使用 Visual Studio 本身进行构建所需的任何命令。

      【讨论】:

      • 请注意:我必须将Release64 切换回Release 才能工作(我猜额外的64 是多余的)
      • 我解释了为什么 Release64 在我的回答中不起作用。
      • 除了将架构写入生成器名称之外,您还可以使用 -A:cmake -G "Visual Studio 15 2017" -A x64 ...
      猜你喜欢
      • 2017-05-31
      • 1970-01-01
      • 2019-02-18
      • 2020-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      相关资源
      最近更新 更多