指定nmake编译架构

修改VC默认架构以指定nmake编译项目的目标架构。

在使用nmake编译protobuf的过程中,出现了LNK4272:库计算机类型“x64”与目标计算机类型“x86”冲突的错误,执行过程:

1
2
3
4
5
6
7
8
9
10
11
12
13

> protobuf\cmake\build\release>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -Dprotobuf_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- The C compiler identification is MSVC 19.23.28106.4
-- The CXX compiler identification is MSVC 19.23.28106.4
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx86/x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx86/x86/cl.exe -- works
……
> protobuf\cmake\build\release>nmake
C:\Program Files\PostgreSQL\9.4\lib\zlib.lib : warning LNK4272:库计算机类型“x64”与目标计算机类型“x86”冲突
tests.exe : fatal error LNK1120: 6 个无法解析的外部命令
NMAKE : fatal error U1077: “"C:\Program Files\CMake\bin\cmake.exe"”: 返回代码“0xffffffff”
Stop.

试着指定x64架构

1
2
3
4
5
6
7
> protobuf\cmake\build\release>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install -A x64 ../..
CMake Error at CMakeLists.txt:16 (project):
Generator
NMake Makefiles
does not support platform specification, but platform
x64
was specified.

nmake不支持指定架构,需要修改VC默认编译架构

1
2
3
4
5
6
> C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build>vcvarsall.bat x64
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.3.5
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'

删掉cmakeCache,重来

1
2
3
4
5
> protobuf\cmake\build\release>cmake -G "NMake Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=../../../../install -Dprotobuf_BUILD_TESTS=OFF ../..
-- The C compiler identification is MSVC 19.23.28106.4
-- The CXX compiler identification is MSVC 19.23.28106.4
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe -- works

确实从x86变成了x64,后续nmake成功。

至于为啥不用cmake生成VS项目sln,因为使用的版本cmake version 3.13.0-rc1还不支持VS2019…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
> protobuf\cmake\build\solution>cmake -G "Visual Studio 16 2019" -DCMAKE_INSTALL_PREFIX=../../../../install ../..
CMake Error: Could not create named generator Visual Studio 16 2019

> protobuf\cmake\build\solution>cmake -G "Visual Studio 14 2015" -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- 这里甚至没有任何提示

> protobuf\cmake\build\solution>cmake -G "Visual Studio 15 2017" -DCMAKE_INSTALL_PREFIX=../../../../install ../..
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
CMake Error at CMakeLists.txt:16 (project):
Failed to run MSBuild command:
MSBuild.exe
to get the value of VCTargetsPath:
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.3.1+1def00d3d
版权所有(C) Microsoft Corporation。保留所有权利。
生成启动时间为 2019/11/4 16:55:45。
节点 1 上的项目“\protobuf\cmake\build\solution\CMakeFiles\3.13.0-rc1\VCTargetsPath.vcxproj”(默认目标)。
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(379,5): error MSB8020: The build tools for Visual Studio 2017 (Platform Toolset = 'v141') cannot be found.