VS2017 开发人员命令提示仅构建到 x86 - 需要 x64

VS2017 developer command prompt only builds to x86 - need x64

我正在尝试使用 cmake 使用 VS2017 开发人员命令提示符构建一些 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 Studio17\Community\VC\Tools\MSVC.16.27023\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio17\Community\VC\Tools\MSVC.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 Studio17\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?

如果您不需要使用 NMake,您可以通过首先生成 64 位来使用 Visual Studio 的构建系统(注意 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 本身生成并执行构建所需的任何命令。

您没有正确使用 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 版本

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

第四

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