cmake protobuf : LNK1112 module machine type 'X86' conflicts with target machine type 'x64'

cmake protobuf LNK1112 module machine type ‘X86’ conflicts with target machine type ‘x64’

20191211 zhangbin

  • 需要cmake 构建visual studio 工程
  • 比如grpc,grpc和依赖库都很大
  • grpc 依赖于protobuf
  • protobuf太大了,不能上传git,所以只能构建了 ,构建只能选cmake

vs2019使用工具链v140

  • 构建环境只有vs2015的工具链
  • 配置没问题
  • -A win32 指定的就是x86 的链接库。
  • cmake 构建命令
calling: ['cmake', '-B', 'H:\\EDUDEV\\EduGrpcBase\\build\\windows\\build\\v140_x86', '-G', 'Visual Studio 16 2019', '-T', 'v140', '-DCMAKE_BUILD_TYPE=Release', '-DSMPSDK_SHARED_LIBRARY=ON', '-A', 'Win32', '../..']
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.

  • 工具链

H:\EDUDEV\EduGrpcBase\build\windows>python build_windows.py -m Release -a x86 -t v140 --dll
BUILD_REST_CLIENT = False
BUILD_GRPC_CLIENT = True
BUILD_HTTP_CPPREST = False
BUILD_WEBSOCKET_CPPREST = False
Building for Arch: x86, Toolset: v140, Mode: Release, DLL: True
calling: ['cmake', '-B', 'H:\\EDUDEV\\EduGrpcBase\\build\\windows\\build\\v140_x86', '-G', 'Visual Studio 16 2019', '-T', 'v140', '-DCMAKE_BUILD_TYPE=Release', '-DSMPSDK_SHARED_LIBRARY=ON', '-A', 'Win32', '../..']
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.18362.
-- The C compiler identification is MSVC 19.0.24234.1
-- The CXX compiler identification is MSVC 19.0.24234.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64_x86/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64_x86/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64_x86/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64_x86/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done

资料

  • 有人说默认是win32,不需要指定,Cmake-gui 确实有这样的情况

  • 我现在是cmake 命令行,情况不同。

  • 构建grpc ,grpc 官方 不推荐cmake

  • protbuf 官方 给出的构建也很简单

fix

  • 问题在这里
  • 我grpc 1.22.0 的protobuf是3.8.0, 对应cmakelist.txt文件并没改变 ,但是有大神提交了patch
  • patch文件
  • 这是因为 set(CMAKE_STATIC_LINKER_FLAGS /ignore:4221") 会覆盖 -A Win32指定的machine,
  • 改为下面的追加就ok了:
 set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} /ignore:4221")

链接成功了

  • 修改
 Building Custom Rule H:/EDUDEV/EduGrpcBase/grpcV1.22.0/third_party/protobuf/cmake/CMakeLists.txt

  • 链接命令
Link:
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64_x86\link.exe /ERRORREPORT:QUEUE /OUT:"H:\EDUDEV\EduGrpcBase\build\windows\build\v140_x86\grpcV1.22.0\Release\grpc_cpp_plugin.exe" /INCREMENTAL:NO /NOLOGO third_party\protobuf\Release\libprotoc.lib
  third_party\protobuf\Release\libprotobuf.lib Release\grpc_plugin_support.lib third_party\protobuf\Release\libprotoc.lib third_party\protobuf\Release\libprotobuf.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg
  32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /PDB:"H:/EDUDEV/EduGrpcBase/build/windows/build/v140_x86/grpcV1.22.0/Release/grpc_cpp_plugin.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"H:/EDUDE
  V/EduGrpcBase/build/windows/build/v140_x86/grpcV1.22.0/Release/grpc_cpp_plugin.lib" /MACHINE:X86 /SAFESEH  /machine:X86 grpc_cpp_plugin.dir\Release\cpp_plugin.obj
  grpc_cpp_plugin.vcxproj -> H:\EDUDEV\EduGrpcBase\build\windows\build\v140_x86\grpcV1.22.0\Release\grpc_cpp_plugin.exe
FinalizeBuildStatus:

  • 编译指令
  cl /c /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\zlib /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0 /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\include /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\boringssl\include /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\protobuf
  /IH:\EDUDEV\EduGrpcBase\build\windows\build\v140_x86\grpcV1.22.0\third_party\zlib /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\benchmark\include /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\cares\cares /IH:\EDUDEV\EduGrpcBase\build\windows\build\v140_x86\g
  rpcV1.22.0\third_party\cares\cares /IH:\EDUDEV\EduGrpcBase\build\windows\build\v140_x86\grpcV1.22.0\third_party\gflags\include /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\address_sorting\include /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\nanopb /IH:\EDU
  DEV\EduGrpcBase\build\windows\build\v140_x86\gens /IH:\EDUDEV\EduGrpcBase\grpcV1.22.0\third_party\protobuf\src /W3 /WX- /O2 /Ob2 /Oy- /D WIN32 /D _WINDOWS /D NDEBUG /D _WIN32_WINNT=0x0100000108030602 /D WIN32_LEAN_AND_MEAN /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NON
  STDC_NO_DEPRECATE /D PB_FIELD_32BIT /D _WIN32_WINNT=0x600 /D _SCL_SECURE_NO_WARNINGS /D _CRT_SECURE_NO_WARNINGS /D _WINSOCK_DEPRECATED_NO_WARNINGS /D "CMAKE_INTDIR=\"Release\"" /D _MBCS /Gm- /EHsc /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /GR /Fo"g
  rpc_cpp_plugin.dir\Release\\" /Fd"grpc_cpp_plugin.dir\Release\vc140.pdb" /Gd /TP /wd4065 /wd4506 /wd4200 /wd4291 /wd4244 /wd4267 /wd4987 /wd4774 /wd4819 /wd4996 /wd4619 /analyze- /errorReport:queue H:\EDUDEV\EduGrpcBase\grpcV1.22.0\src\compiler\cpp_plugin.cc
  cpp_plugin.cc

 libprotoc.dir\Release\subprocess.obj
  libprotoc.dir\Release\zip_writer.obj
  libprotoc.vcxproj -> H:\EDUDEV\EduGrpcBase\build\windows\build\v140_x86\grpcV1.22.0\third_party\protobuf\Release\libprotoc.lib
FinalizeBuildStatus:
  正在删除文件“libprotoc.dir\Release\libprotoc.tlog\unsuccessfulbuild”。
  正在对“libprotoc.dir\Release\libprotoc.tlog\libprotoc.lastbuildstate”执行 Touch 任务。
已完成生成项目“H:\EDUDEV\EduGrpcBase\build\windows\build\v140_x86\grpcV1.22.0\third_party\protobuf\libprotoc.vcxproj”(默认目标)的操作。

发布了664 篇原创文章 · 获赞 55 · 访问量 217万+

猜你喜欢

转载自blog.csdn.net/commshare/article/details/103490023