Simple installation method of GRPC C++ windows

Recently, because I want to add a grpc entry to Llama.cpp, I have tossed about the installation of the GRPC runtime. At first, I referred to the official GRPC Build from source , but failed.

The main reason is that the cmake-based installation and invocation encountered several major problems.

One is the problem of vscode compiler integration, and the other is the search path problem of cmake's find_package, etc. In the end, it was unsuccessful. Later, after further searching, I found a very simple method:

Install using vcpkg

vcpkg is an official package manager from Microsoft, which can be used with cmake

microsoft/vcpkg: C++ Library Manager for Windows, Linux, and MacOS (github.com) icon-default.png?t=N4P3https://github.com/microsoft/vcpkg First we need to complete the installation (it is recommended to readthe official Chinese guide), after completing the download of the basic software package , do the following in order

git clone https://github.com/microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat

You can complete the download and installation. It is best to choose a disk with a relatively large space to execute this command, because this directory will also be used as the installation path of each package downloaded by vcpkg.

After execution, add .\vcpkg to the PATH in the environment variable, and it can be executed directly next time.

Install grpc using vcpkg

install grpc

vcpkg install grpc:x64-windows

The latest version is the default x64, you can also directly enter

vcpkg install grpc

This default setting seems to have been changed only recently. I see that many materials on the Internet need to add the suffix x64-windows. You can pay attention to the following tips during the installation process.

Install protobuf

vcpkg install protobuf protobuf:x64-windows

Use the installation package in the cpp project

After installation, if you want to find this package directly through find_package in the cmake project, you need to do two things.

Do the following

vcpkg integrate install

After execution, a prompt will appear

Applied user-wide integration for this vcpkg root.
CMake projects should use: "-DCMAKE_TOOLCHAIN_FILE=E:/xxx/vcpkg.cmake"

All MSBuild C++ projects can now #include any installed libraries. Linking will be handled automatically. Installing new libraries will make them instantly available.

The green part is the additional parameters we need when we execute the cmake build, for example, I will need to do when I run llama

cmake .. -DCMAKE_TOOLCHAIN_FILE=E:/xxx/vcpkg.cmake

The compilation stage after the build is complete is the same as the original.

Reference the relevant header files

The cmakefile of grpc mainly includes the following parts, this is the cmakefile of grpc-server I added for llama.cpp

# 生成文件的名称
set(TARGET grpc-server)

# 引入包
find_package(Protobuf CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)

# 引入protoc和grpc_cpp_plugin程序,在编译proto文件的阶段需要使用
find_program(_PROTOBUF_PROTOC protoc)
find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin)

# 引入必要的头文件搜索路径
# 生成路径,因为proto编译后的c和h文件在这个目录,所以必须包含
include_directories(${CMAKE_CURRENT_BINARY_DIR})
# protobuf头文件路径
include_directories(${Protobuf_INCLUDE_DIRS})

message(STATUS "Using protobuf ${Protobuf_VERSION} ${Protobuf_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR}")


# 寻找proto文件,按你的文件名称修改
get_filename_component(hw_proto "./message.proto" ABSOLUTE)
# 寻找路径
get_filename_component(hw_proto_path "${hw_proto}" PATH)

# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/message.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/message.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/message.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/message.grpc.pb.h")

# 编译proto得到h和c文件
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}")

# 添加生成的文件,作为类库
add_library(hw_grpc_proto
  ${hw_grpc_srcs}
  ${hw_grpc_hdrs}
  ${hw_proto_srcs}
  ${hw_proto_hdrs})

add_executable(${TARGET} grpc-server.cpp)
target_link_libraries(${TARGET} PRIVATE common llama ${CMAKE_THREAD_LIBS_INIT} hw_grpc_proto absl::flags
  absl::flags_parse
  gRPC::grpc++_reflection
  gRPC::grpc++
  protobuf::libprotobuf)
target_compile_features(${TARGET} PRIVATE cxx_std_11)
if(TARGET BUILD_INFO)
  add_dependencies(${TARGET} BUILD_INFO)
endif()

Guess you like

Origin blog.csdn.net/baijiafan/article/details/130935982