如何使用VCPKG工具链删除-plugin = protoc-gen-grpc的硬编码路径

发布于 2025-01-22 08:31:19 字数 1620 浏览 1 评论 0原文

我有一个使用CMAKE在C ++中构建GRPC库的工作示例。 但是,我需要编码标志的绝对路径 - 插件= protoc-gen-grpc =“/home/home/myusername/dev/lab/minigl/vcpkg/nasterned/x64-linux/tools/grpc/grpc/grpc/grpc_cpp_plugin”

我真正想做的事是为了在下面的grpc_cpp_plugin中找到类似于环境变量$ {protobuf_protoc_executable}的内容,但我找不到它。即$ {gprc_cpp_plugin_executable}

有人知道我是否在这里错过了一些简单的东西?

这是我整个cmakelists.txt的列表

project(hello_contract_library)

set(CMAKE_VERBOSE_MAKEFILE ON)

find_package(protobuf CONFIG REQUIRED)
#target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

set(GENERATED_DIR "${PROJECT_SOURCE_DIR}/generated")
file(MAKE_DIRECTORY "${GENERATED_DIR}")

set(hello_src "${GENERATED_DIR}/hello.pb.cc")
set(hello_hdr "${GENERATED_DIR}/hello.pb.h")
set(hello_grpc_src  "${GENERATED_DIR}/hello.grpc.pb.cc")
set(hello_grpc_hdr  "${GENERATED_DIR}/hello.grpc.pb.h")

# Added to include generated header files
include_directories("${GENERATED_DIR}")

add_custom_command(
      OUTPUT "${hello_src}" "${hello_hdr}" "${hello_grpc_src}" "${hello_grpc_hdr}"
      COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
      ARGS --grpc_out "${PROJECT_SOURCE_DIR}/generated"
        --cpp_out "${PROJECT_SOURCE_DIR}/generated"
        -I "/home/myusername/dev/lab/MiniGL/src/backend/public/contracts"
        --plugin=protoc-gen-grpc="/home/myusername/dev/lab/MiniGL/vcpkg/installed/x64-linux/tools/grpc/grpc_cpp_plugin"
        "hello.proto"
      DEPENDS "hello.proto")

add_library(${PROJECT_NAME}
  ${hello_src}
  ${hello_hdr}
  ${hello_grpc_src}
  ${hello_grpc_hdr})

I have a working example of building a grpc library in C++ using CMake.
However I need to hardcode the absolute path to the flag
--plugin=protoc-gen-grpc="/home/myusername/dev/lab/MiniGL/vcpkg/installed/x64-linux/tools/grpc/grpc_cpp_plugin"

What I really would like to do is to find something like the environment variable ${PROTOBUF_PROTOC_EXECUTABLE} below for the grpc_cpp_plugin that is required but I just can't find it. i.e. ${GPRC_CPP_PLUGIN_EXECUTABLE}

Does anyone know if I have missed something simple here?

This is my entire listing of the CMakeLists.txt

project(hello_contract_library)

set(CMAKE_VERBOSE_MAKEFILE ON)

find_package(protobuf CONFIG REQUIRED)
#target_link_libraries(main PRIVATE protobuf::libprotoc protobuf::libprotobuf protobuf::libprotobuf-lite)

set(GENERATED_DIR "${PROJECT_SOURCE_DIR}/generated")
file(MAKE_DIRECTORY "${GENERATED_DIR}")

set(hello_src "${GENERATED_DIR}/hello.pb.cc")
set(hello_hdr "${GENERATED_DIR}/hello.pb.h")
set(hello_grpc_src  "${GENERATED_DIR}/hello.grpc.pb.cc")
set(hello_grpc_hdr  "${GENERATED_DIR}/hello.grpc.pb.h")

# Added to include generated header files
include_directories("${GENERATED_DIR}")

add_custom_command(
      OUTPUT "${hello_src}" "${hello_hdr}" "${hello_grpc_src}" "${hello_grpc_hdr}"
      COMMAND ${PROTOBUF_PROTOC_EXECUTABLE}
      ARGS --grpc_out "${PROJECT_SOURCE_DIR}/generated"
        --cpp_out "${PROJECT_SOURCE_DIR}/generated"
        -I "/home/myusername/dev/lab/MiniGL/src/backend/public/contracts"
        --plugin=protoc-gen-grpc="/home/myusername/dev/lab/MiniGL/vcpkg/installed/x64-linux/tools/grpc/grpc_cpp_plugin"
        "hello.proto"
      DEPENDS "hello.proto")

add_library(${PROJECT_NAME}
  ${hello_src}
  ${hello_hdr}
  ${hello_grpc_src}
  ${hello_grpc_hdr})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2025-01-29 08:31:19

暂时获取此问题的唯一方法是创建您自己的.cmake文件,该文件查找这些可执行文件并定义此变量。幸运的是,Google有一个示例在这里。您可以将common.cmake复制到cmake您将包含在项目中的目录。

这将为您提供$ {_ grpc_cpp_plugin_executable}- plugin and $ {_ grpc_grpcpp}将放入targe> target_link_libraries 。

的示例

原始目录中的根

# common project setup

include(cmake/common.cmake) # comes from Google examples
add_subdirectory(proto)

目录中

# Proto files
set(my_protos
    test.proto
)

# Generated sources
set(my_protos_srcs
    ${CMAKE_BINARY_DIR}/proto/test.pb.cc
)
set(my_protos_hdrs
    ${CMAKE_BINARY_DIR}/proto/test.pb.h
)

add_custom_command(
    OUTPUT ${my_protos_srcs} ${my_protos_hdrs}
    COMMAND ${_PROTOBUF_PROTOC}
    ARGS --cpp_out ${CMAKE_BINARY_DIR}/proto
        --grpc_out ${CMAKE_BINARY_DIR}/proto
        -I ${CMAKE_SOURCE_DIR}/proto
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        ${my_protos} 
    DEPENDS ${my_protos}
)

add_library(protos
    ${my_protos_srcs}
    ${my_protos_hdrs}
)
target_link_libraries(protos
    ${_PROTOBUF_LIBPROTOBUF}
    ${_GRPC_GRPCPP}
)

The only way to get this for now is by creating your own .cmake file which looks for these executables and define this variables. Fortunately Google has an example here. You can copy the common.cmake to a cmake directory that you will include to the project.

This will give you ${_GRPC_CPP_PLUGIN_EXECUTABLE} for your --plugin and ${_GRPC_GRPCPP} to put in your target_link_libraries.

Example

In root directory

# common project setup

include(cmake/common.cmake) # comes from Google examples
add_subdirectory(proto)

In proto directory

# Proto files
set(my_protos
    test.proto
)

# Generated sources
set(my_protos_srcs
    ${CMAKE_BINARY_DIR}/proto/test.pb.cc
)
set(my_protos_hdrs
    ${CMAKE_BINARY_DIR}/proto/test.pb.h
)

add_custom_command(
    OUTPUT ${my_protos_srcs} ${my_protos_hdrs}
    COMMAND ${_PROTOBUF_PROTOC}
    ARGS --cpp_out ${CMAKE_BINARY_DIR}/proto
        --grpc_out ${CMAKE_BINARY_DIR}/proto
        -I ${CMAKE_SOURCE_DIR}/proto
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        ${my_protos} 
    DEPENDS ${my_protos}
)

add_library(protos
    ${my_protos_srcs}
    ${my_protos_hdrs}
)
target_link_libraries(protos
    ${_PROTOBUF_LIBPROTOBUF}
    ${_GRPC_GRPCPP}
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文