美文网首页
windows环境编译gRPC并使用CMakeList创建项目

windows环境编译gRPC并使用CMakeList创建项目

作者: ogood | 来源:发表于2021-03-30 06:28 被阅读0次

    准备工作

    • Install Visual Studio
    • Install Git.
    • Install CMake.
    • Install nasm and add it to PATH (choco install nasm) - required by boringssl
      也可以将nasm路径添加到CMakeCache文件

    去github下载grpc的源码

    git clone --recurse-submodules -b v1.35.0 https://github.com/grpc/grpc
    
    or
    
    > git clone -b RELEASE_TAG_HERE https://github.com/grpc/grpc
    > cd grpc
    > git submodule update --init
    

    打开CMD,cd到源代码的build目录

    cmake -DgRPC_INSTALL=ON -DgRPC_BUILD_TESTS=OFF -DCMAKE_INSTALL_PREFIX=C:\\Users\\Y\\Documents\\code\\grpc\\install DCMAKE_BUILD_TYPE=Debug ../ -A Win32
    
    

    然后打开VS,全部生成,选择INSTALL,生成。下一步编写CMakelist.txt创建helloworld项目

    # Minimum CMake required
    cmake_minimum_required(VERSION 3.15)
    
    # Project
    project(stringreverse)
    
    set(CMAKE_PREFIX_PATH  
    #C:\\Users\\Documents\\code\\grpc\\build32\\Release
    C:\\Users\\Documents\\code\\grpc\\install\\bin
    C:\\Users\\Documents\\code\\grpc\\install\\lib
    C:\\Users\\Documents\\code\\grpc\\install\\cmake
    C:\\Users\\Documents\\code\\grpc\\install\\lib\\cmake\\grpc
    )
    # Protobuf
    #set(protobuf_MODULE_COMPATIBLE TRUE)
    find_package(Protobuf CONFIG REQUIRED)
    message(STATUS "Using protobuf ${protobuf_VERSION}")
    
    # Protobuf-compiler
    set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)
    
    # gRPC
    find_package(gRPC CONFIG REQUIRED)
    message(STATUS "Using gRPC ${gRPC_VERSION}")
    set(_GRPC_GRPCPP gRPC::grpc++)
    set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)
    find_package(Threads REQUIRED)
    # Proto file
    get_filename_component(hw_proto "stringreverse.proto" ABSOLUTE)
    get_filename_component(hw_proto_path "${hw_proto}" PATH)
    
    # Generated sources
    set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/stringreverse.pb.cc")
    set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/stringreverse.pb.h")
    set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/stringreverse.grpc.pb.cc")
    set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/stringreverse.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 generated *.pb.h files
    include_directories("${CMAKE_CURRENT_BINARY_DIR}")
    
    # Targets (client|server)
    foreach(_target
      client server)
      add_executable(${_target} "${_target}.cc"
        ${hw_proto_srcs}
        ${hw_grpc_srcs})
      target_link_libraries(${_target}
        ${_REFLECTION}
        ${_GRPC_GRPCPP}
        ${_PROTOBUF_LIBPROTOBUF})
    endforeach()
    
    

    相关文章

      网友评论

          本文标题:windows环境编译gRPC并使用CMakeList创建项目

          本文链接:https://www.haomeiwen.com/subject/tmhwhltx.html