簡體   English   中英

使用 cmake 為 msys2/mingw64 構建 gRPC 和 openssl 作為依賴項失敗,引用未定義

[英]Building gRPC and openssl as dependency with cmake for msys2/mingw64 fails with undefined references

我正在嘗試使用更改的文件樹結構和我自己的帶有 msys2/mingw64 的 CMakeLists.txt 構建 grpc helloworld 示例。 我想使用 add_subdirectory 從源構建 grpc 作為 static 庫,並從源遞歸地構建 grpc 的依賴項。

這樣做我得到了未定義引用的鏈接錯誤。 經過一些研究,似乎 boringssl 在 msys2/mingw64 中構建時存在問題,或者至少 boringssl 包含在 grpc 的 repo 中的方式與 msys2/mingw64 不兼容。

由於 msys2 中的 grpc package 是使用 openssl 作為依賴項構建的,因此我決定在構建期間從源代碼構建 openssl 並讓 grpc 靜態鏈接到它。 我希望在沒有 boringssl 子模塊的情況下克隆 grpc 就足夠了。

當 openssl 動態鏈接到 libssl.dll.a 時,我能夠成功鏈接並運行示例。 但是,當我嘗試鏈接到 static openssl 構建時,我再次遇到未定義的引用錯誤。`grpc_test2_server.exe

這是我得到的鏈接錯誤:

cmd.exe /C "cd . && C:\msys64\mingw64\bin\c++.exe -g  @CMakeFiles\grpc_test2_server.rsp -o grpc_test2_server.exe -Wl,--out-implib,libgrpc_test2_server.dll.a -Wl,--major-image-version,0,--minor-image-version,0  && cd ."
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_addr.o): in function `addr_strings':    
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_addr.c:208: undefined reference to `__imp_getnameinfo'     
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_addr.c:220: undefined reference to `gai_strerrorW'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_addr.o): in function `BIO_lookup_ex':   
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_addr.c:731: undefined reference to `gai_strerrorW'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock.o): in function `BIO_sock_error':  
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock.c:99: undefined reference to `__imp_getsockopt'       
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock.o): in function `BIO_gethostbyname':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock.c:113: undefined reference to `__imp_gethostbyname'   
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock2.o): in function `BIO_listen':     
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock2.c:217: undefined reference to `__imp_getsockopt'     
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(b_sock2.o): in function `BIO_accept_ex':  
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/crypto/bio/b_sock2.c:290: undefined reference to `__imp_accept'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_get_prov_info':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1281: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1289: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_cert_get_fname':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1329: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1335: undefined reference to `__imp_CertGetCertificateContextProperty'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_open_store': 
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1400: undefined reference to `__imp_CertOpenStore'     
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_list_certs': 
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1432: undefined reference to `__imp_CertFreeCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1435: undefined reference to `__imp_CertEnumCertificatesInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1443: undefined reference to `__imp_CertCloseStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_find_cert':  
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1455: undefined reference to `__imp_CertFindCertificateInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1459: undefined reference to `__imp_CertEnumCertificatesInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_find_key':   
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1559: undefined reference to `__imp_CertFreeCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1561: undefined reference to `__imp_CertCloseStore'
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1593: undefined reference to `__imp_CertFreeCertificateC:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: third-party/openssl/lib/libcrypto.a(e_capi.o): in function `capi_load_ssl_client_cert':
C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1720: undefined reference to `__imp_CertEnumCertificatesInStore'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1740: undefined reference to `__imp_CertDuplicateCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1754: undefined reference to `__imp_CertFreeCertificateContext'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\PatVax\Workspace\cpp\grpc_test2\build-debug\third-party\openssl_build/../../../third-party/openssl/engines/e_capi.c:1756: undefined reference to `__imp_CertCloseStore'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.`

這是我目前正在使用的 CMakeLists.txt:

cmake_minimum_required(VERSION 3.24)
project(grpc_test2 C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_C_STANDARD 17)

set(BUILD_SHARED_LIBS OFF)
set(gRPC_BUILD_SHARED_LIBS OFF)

set(gRPC_BUILD_GRPC_CSHARP_EXT OFF)
set(gRPC_BUILD_GRPC_PHP_EXT OFF)
set(gRPC_BUILD_GRPC_PYTHON_EXT OFF)
set(gRPC_BUILD_GRPC_NODE_EXT OFF)
set(gRPC_BUILD_GRPC_RUBY_EXT OFF)
set(gRPC_BUILD_GRPC_OBJC_EXT OFF)
set(gRPC_BUILD_GRPC_OBJC_PLUGIN OFF)

set(_gRPC_BASELIB_LIBRARIES libws2_32 libcrypt32)

include(ProcessorCount)
ProcessorCount(N)

set(OPENSSL_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third-party/openssl)
set(OPENSSL_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/third-party/openssl_build)
set(OPENSSL_INSTALL_DIR ${CMAKE_CURRENT_BINARY_DIR}/third-party/openssl)
set(OPENSSL_INCLUDE_DIR ${OPENSSL_INSTALL_DIR}/include)
set(MSYS_SHELL ${MSYS2_ROOT}/msys2_shell.cmd)
if(CMAKE_BUILD_TYPE STREQUAL "Release")
  set(OPENSSL_CONFIGURE_COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c 
    "${OPENSSL_SOURCE_DIR}/Configure --prefix=${OPENSSL_INSTALL_DIR} --openssldir=${OPENSSL_INSTALL_DIR} --release -static no-shared mingw64")
elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
  set(OPENSSL_CONFIGURE_COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c 
    "${OPENSSL_SOURCE_DIR}/Configure --prefix=${OPENSSL_INSTALL_DIR} --openssldir=${OPENSSL_INSTALL_DIR} --debug -static no-shared mingw64")
else()
  set(OPENSSL_CONFIGURE_COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c 
    "${OPENSSL_SOURCE_DIR}/Configure --prefix=${OPENSSL_INSTALL_DIR} --openssldir=${OPENSSL_INSTALL_DIR} -static no-shared mingw64")
endif()

add_custom_target(OpenSSL_Configured
  COMMAND ${OPENSSL_CONFIGURE_COMMAND}
  WORKING_DIRECTORY ${OPENSSL_BINARY_DIR}
  BYPRODUCTS ${OPENSSL_BINARY_DIR}/Makefile
  DEPENDS ${OPENSSL_SOURCE_DIR}/Configure
  COMMENT "OpenSSL is configuring")

add_custom_target(OpenSSL_Built
  COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c "make -j${N}"
  WORKING_DIRECTORY ${OPENSSL_BINARY_DIR}
  BYPRODUCTS ${OPENSSL_BINARY_DIR}/libssl.a ${OPENSSL_BINARY_DIR}/libcrypto.a
  DEPENDS OpenSSL_Configured
  COMMENT "OpenSSL is building")

add_custom_target(OpenSSL_Installed
  COMMAND ${MSYS_SHELL} -mingw64 -no-start -here -defterm -c "make install"
  WORKING_DIRECTORY ${OPENSSL_BINARY_DIR}
  BYPRODUCTS ${OPENSSL_INSTALL_DIR}/lib/libssl.a ${OPENSSL_INSTALL_DIR}/lib/libcrypto.a ${OPENSSL_INSTALL_DIR}/lib/libssl.dll.a ${OPENSSL_INSTALL_DIR}/lib/libcrypto.dll.a ${OPENSSL_INSTALL_DIR}/lib/libssl.dll ${OPENSSL_INSTALL_DIR}/lib/libcrypto.dll
  DEPENDS OpenSSL_Built
  COMMENT "OpenSSL is installing")

add_custom_target(OpenSSL DEPENDS OpenSSL_Installed COMMENT "OpenSSL is ready")

file(MAKE_DIRECTORY ${OPENSSL_INCLUDE_DIR})

add_library(OpenSSL::SSL STATIC IMPORTED GLOBAL)
set_property(TARGET OpenSSL::SSL PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libssl.a)
set_property(TARGET OpenSSL::SSL PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(OpenSSL::SSL OpenSSL)

add_library(OpenSSL::Crypto STATIC IMPORTED GLOBAL)
set_property(TARGET OpenSSL::Crypto PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libcrypto.a)
set_property(TARGET OpenSSL::Crypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(OpenSSL::Crypto OpenSSL)

add_library(ssl STATIC IMPORTED GLOBAL)
set_property(TARGET ssl PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libssl.a)
set_property(TARGET ssl PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(ssl OpenSSL)

add_library(crypto STATIC IMPORTED GLOBAL)
set_property(TARGET crypto PROPERTY IMPORTED_LOCATION ${OPENSSL_INSTALL_DIR}/lib/libcrypto.a)
set_property(TARGET crypto PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${OPENSSL_INCLUDE_DIR})
add_dependencies(crypto OpenSSL)

set(_gRPC_SSL_LIBRARIES ssl crypto)
set(_gRPC_SSL_INCLUDE_DIR ${OPENSSL_INCLUDE_DIR})

add_subdirectory(third-party/grpc ${CMAKE_CURRENT_BINARY_DIR}/third-party/grpc)

get_filename_component(hw_proto "${CMAKE_CURRENT_SOURCE_DIR}/protobufs/helloworld.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)

find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)

set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/helloworld.grpc.pb.h")
add_custom_command(
      OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
      COMMAND protobuf::protoc
      ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
        -I "${hw_proto_path}"
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        "${hw_proto}"
      DEPENDS "${hw_proto}")

include_directories("${CMAKE_CURRENT_BINARY_DIR}")

add_library(hw_grpc_proto
  ${hw_grpc_srcs}
  ${hw_grpc_hdrs}
  ${hw_proto_srcs}
  ${hw_proto_hdrs})
target_link_libraries(hw_grpc_proto grpc++ grpc++_reflection libprotobuf)

add_executable(${CMAKE_PROJECT_NAME}_server server.cpp)
add_executable(${CMAKE_PROJECT_NAME}_client client.cpp)
target_link_libraries(${CMAKE_PROJECT_NAME}_server grpc++ grpc++_reflection libprotobuf hw_grpc_proto)
target_link_libraries(${CMAKE_PROJECT_NAME}_client grpc++ grpc++_reflection libprotobuf hw_grpc_proto)

我的目錄結構如下所示:

在此處輸入圖像描述

  • grpc 與所有子模塊一起克隆,沒有 boringssl
  • openssl 與所有子模塊一起克隆
  • protobufs 包含在構建過程中正在編譯的 .proto 文件
  • .cpp 文件的內容與 grpc repo 中的 helloworld 示例中的內容相同

我的問題可能是什么原因? 我錯過了什么嗎? 提前致謝。

EDIT3: 我實際上在這里意識到了一個大錯誤。 您不能鏈接 ws2_32.lib ,因為它是用 MSVC 而不是 MinGW 構建的。 但是,您的問題源於這里。

我覺得自己像頭驢,但顯然較新版本的 MSYS2/MinGW應該能夠鏈接到這些 static 庫。 所以下面的解決方案值得一試。


編輯:經過深入研究,我想我找到了支持 MSVC 而不支持 mingw 的原因。 您遇到問題的原因是因為他們很可能使用#pragma鏈接他們的庫(這是 MSVC 特定的功能)。

嘗試額外鏈接到這個庫: ws2_32 (也許 64 位版本稱為ws2_64 )-(更准確地說,你想鏈接到 winsock2 64 位版本)

EDIT2:它們都被命名為ws2_32#pragma為您選擇正確的,即唯一不同的是正確的路徑 SDK


基於這個官方 github 問題 GRPC團隊不支持 MinGW Windows 構建。

他們描述了與您描述的相同的問題,並且他們還就如何修復boringssl的工作提出了建議,即

長話短說,如果我是你,我會在 Windows 上切換到 MSVC - 如果你的目標平台是 Windows,我看不出為什么不使用它。

編輯:但是,如果您仍然堅持使用 MinGW,那么也許會讓您更接近如何解決您的問題,我可能會指出gai_strerrorW是 ws2tcpip.h 中定義的 function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM