準備工作
- 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創(chuàng)建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()