应用程序代码外部的CMake协议

应用程序代码外部的CMake协议,cmake,protocol-buffers,external,Cmake,Protocol Buffers,External,基本上,我希望我的结构如下: MainRepo +---app1 +---app2 +---common +---some_lib1 +---some_lib2 +---protobuf +---comms.proto +---cs +---Comms.pb.cs +---cpp

基本上,我希望我的结构如下:

MainRepo
    +---app1
    +---app2
    +---common
        +---some_lib1
        +---some_lib2
        +---protobuf
                +---comms.proto
                +---cs
                    +---Comms.pb.cs
                +---cpp
                    +---comms.pb.cc
                    +---comms.pb.h
我希望能够查看repo,并拥有一个脚本,为repo中的应用程序运行所有不同语言的protoc。这是一个mono repo,包含两个不同arm机器的应用程序和一个x64。我基本上是在运行protoc,它为c、js、cs、cpp等生成所有源文件,并将它们放在protobuf下的文件夹中

我想有一个APP1,例如,去找C++头和源,并用它们来构建应用程序。目前,我正在破解的示例使用cmake生成.cc和.h,这给我带来了不便,因为intellisense抱怨说,这些文件在我写作时并不存在

不管怎样,我整天都在窃听cmake。我总是以cmake出现转发声明错误而告终,无法编译my.cc和.h

PROJECT(test)

CMAKE_MINIMUM_REQUIRED (VERSION 2.6)

SET(CMAKE_CXX_FLAGS "-g -Wall -Werror -std=c++11")

ADD_EXECUTABLE(main main.cpp)

TARGET_LINK_LIBRARIES(main proto ${PROTOBUF_LIBRARY})

find_package(Protobuf REQUIRED)

set(PROTOBUF_IMPORT_DIRS "../proto")
set (msgs ${PROTOBUF_IMPORT_DIRS}/communications.proto)
PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${msgs})
add_library(proto SHARED ${PROTO_SRCS})
target_link_libraries(proto ${PROTOBUF_LIBRARY})
不知道有没有什么建议。我不想把我的protobuf文件放在我的公共文件夹之外,我也不需要protoc来生成这些文件,因为我用另一种方式(尽管我可以改变这种方式)。我只想确保特定语言的文件仍然可以查看,而不仅仅是在cmake期间生成的文件,我无法查看它们。

我的2美分

这是我为谷歌/或工具所做的 参考:

Protobuf积分 我正在使用
Fetchcontent()
(CMake>=3.18以拥有SOURCE_SUBDIR选项IIRC),但我还需要对其进行修补(例如,拥有CMP0077) 您可以在此处找到protobuf修补程序:

cmake/dependencies/CMakeLists.txt

include(FetchContent)
set(BUILD_SHARED_LIBS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(BUILD_TESTING OFF)

message(CHECK_START "Fetching Protobuf")
list(APPEND CMAKE_MESSAGE_INDENT "  ")
set(protobuf_BUILD_TESTS OFF)
set(protobuf_BUILD_EXPORT OFF)
set(protobuf_MSVC_STATIC_RUNTIME OFF)
# FetchContent_Declare(SOURCE_SUBDIR) was introduced in 3.18
FetchContent_Declare(
  protobuf
  GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git"
  GIT_TAG "v3.12.2"
  PATCH_COMMAND git apply "${CMAKE_CURRENT_LIST_DIR}/../../patches/protobuf-v3.12.2.patch"
  SOURCE_SUBDIR cmake)
FetchContent_MakeAvailable(protobuf)
list(POP_BACK CMAKE_MESSAGE_INDENT)
message(CHECK_PASS "fetched")
参考:

注意:对于cmake<3.18,我使用
ExternalProject
+
execute\u process()
见:

生成Protobuf文件 由于我们已经集成了Protobuf,现在我们可以访问
Protobuf::protoc
。 要构建proto文件,只需调整

# Get Protobuf include dirs
get_target_property(protobuf_dirs protobuf::libprotobuf INTERFACE_INCLUDE_DIRECTORIES)
foreach(dir IN LISTS protobuf_dirs)
  if ("${dir}" MATCHES "BUILD_INTERFACE")
    message(STATUS "Adding proto path: ${dir}")
    list(APPEND PROTO_DIRS "--proto_path=${dir}")
  endif()
endforeach()

# Generate Protobuf cpp sources
set(PROTO_HDRS)
set(PROTO_SRCS)
file(GLOB_RECURSE proto_files RELATIVE ${PROJECT_SOURCE_DIR}
  "common/protobuf/*.proto"
  )

foreach(PROTO_FILE IN LISTS proto_files)
  #message(STATUS "protoc proto(cc): ${PROTO_FILE}")
  get_filename_component(PROTO_DIR ${PROTO_FILE} DIRECTORY)
  get_filename_component(PROTO_NAME ${PROTO_FILE} NAME_WE)
  set(PROTO_HDR ${PROJECT_BINARY_DIR}/${PROTO_DIR}/${PROTO_NAME}.pb.h)
  set(PROTO_SRC ${PROJECT_BINARY_DIR}/${PROTO_DIR}/${PROTO_NAME}.pb.cc)
  #message(STATUS "protoc hdr: ${PROTO_HDR}")
  #message(STATUS "protoc src: ${PROTO_SRC}")
  add_custom_command(
    OUTPUT ${PROTO_SRC} ${PROTO_HDR}
    COMMAND protobuf::protoc
    "--proto_path=${PROJECT_SOURCE_DIR}"
    ${PROTO_DIRS}
    "--cpp_out=${PROJECT_BINARY_DIR}"
    ${PROTO_FILE}
    DEPENDS ${PROTO_FILE} protobuf::protoc
    COMMENT "Generate C++ protocol buffer for ${PROTO_FILE}"
    VERBATIM)
  list(APPEND PROTO_HDRS ${PROTO_HDR})
  list(APPEND PROTO_SRCS ${PROTO_SRC})
endforeach()
参考:

在您可以使用
PROTO_HDR
PROTO_SRC
之后,例如,将它们添加到您的目标源中等

注:对于.Net,您可以查看


总之,只需根据您的需要进行调整,例如,在源代码树中生成而不是二进制目录等,您应该能够做任何您想做的事情…

我尝试了您的cmake流程。我仍然得到AuxillaryParseTableField google::protobuf::internal没有命名类型。我的cmake文件仍然是一个向前声明的问题吗?这一切都起作用了。显然,在12.3中,有一个拼写错误是用辅助词修复的,这导致我在处理cmake问题时走错了路。你的流量工作得很好。非常感谢。