C++ 原始图书馆的CMake

C++ 原始图书馆的CMake,c++,cmake,protocol-buffers,proto,C++,Cmake,Protocol Buffers,Proto,我有两个跨两个不同文件夹的原型文件,我正试图使用CMake来构建整个项目 protofile1将protofile2作为其依赖项 Library1将protofile1作为其依赖项,我可以使用protobuf\u generate\u cpp生成它 但是为了生成protofile1,我将protofile2作为它的依赖项。我如何使用CMake实现这一点 如何编译proto文件并使用CMake(在folder2中)将其作为库提供 文件夹结构: | |-folder1 ---|-protofil

我有两个跨两个不同文件夹的原型文件,我正试图使用CMake来构建整个项目

  • protofile1将protofile2作为其依赖项
  • Library1将protofile1作为其依赖项,我可以使用
    protobuf\u generate\u cpp
    生成它
但是为了生成protofile1,我将protofile2作为它的依赖项。我如何使用CMake实现这一点

如何编译proto文件并使用CMake(在folder2中)将其作为库提供

文件夹结构:

|
|-folder1
---|-protofile1.proto
---|-library1.cc
|-folder2
---|-protofile2.proto
---|-library2.cc
用于文件夹1的CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
find_package(Protobuf REQUIRED)
protobuf_generate_cpp(protofile1_cc protofile1_header protofile1.proto)
target_link_libraries(protofile1_cc INTERFACE protofile2_lib) # is this correct?

add_library(library1 INTERFACE)
target_sources(library1 INTERFACE library1.cc)
target_link_libraries(library1 INTERFACE protofile1_cc)
用于文件夹2的CMakeLists.txt

cmake_minimum_required(VERSION 3.3)
find_package(Protobuf REQUIRED)
# don't know how to do this
add_library(protofile2_lib INTERFACE) # is this correct?
target_sources(protofile2_lib INTERFACE protofile2.proto) # is this correct?
该命令不定义目标,但定义了引用自动生成的源文件(
.cc
.h
)的CMake变量。这些变量应用于通过
add\u library()
定义目标。您的做法是正确的,但是
folder2
中的CMakeLists.txt文件也应该调用
protobuf\u generate\u cpp
来处理
protofile2.proto

此外,如果您使用CMake同时构建这两个组件,则顶级CMake(位于
folder1
folder2
的父文件夹中)可以找到Protobuf,因此您不必两次找到它。类似这样的内容应该会让您更接近所需的解决方案:

CMakeLists.txt(顶级):

folder2/CMakeLists.txt

# Auto-generate the source files for protofile2.
protobuf_generate_cpp(protofile2_cc protofile2_header protofile2.proto)
# Use the CMake variables to add the generated source to the new library.
add_library(protofile2_lib SHARED ${protofile2_cc} ${protofile2_header})
# Link the protobuf libraries to this new library.
target_link_libraries(protofile2_lib PUBLIC ${Protobuf_LIBRARIES})
# Auto-generate the source files for protofile1.
protobuf_generate_cpp(protofile1_cc protofile1_header protofile1.proto)
# Use the CMake variables to add the generated source to the new library.
add_library(library1 SHARED ${protofile1_cc} ${protofile1_header})
# Link proto2 library to library1.
target_link_libraries(library1 INTERFACE protofile2_lib)
folder1/CMakeLists.txt

# Auto-generate the source files for protofile2.
protobuf_generate_cpp(protofile2_cc protofile2_header protofile2.proto)
# Use the CMake variables to add the generated source to the new library.
add_library(protofile2_lib SHARED ${protofile2_cc} ${protofile2_header})
# Link the protobuf libraries to this new library.
target_link_libraries(protofile2_lib PUBLIC ${Protobuf_LIBRARIES})
# Auto-generate the source files for protofile1.
protobuf_generate_cpp(protofile1_cc protofile1_header protofile1.proto)
# Use the CMake variables to add the generated source to the new library.
add_library(library1 SHARED ${protofile1_cc} ${protofile1_header})
# Link proto2 library to library1.
target_link_libraries(library1 INTERFACE protofile2_lib)

但问题是protofile1将protofile2作为其依赖项,因此在构建过程中需要它。怎么办?您建议将proto2库与library1链接。相反,proto1需要proto2。library1需要proto1。@Chinmaysah这就是我设置依赖项的方式。如果有其他源要添加到
library1
,则可以简单地将这些源附加到
add\u library()
命令中。protofile1可以在“构建期间”,特别是在链接阶段访问protofile2实现。您还需要protofile2头吗?我不确定你所说的“构建期间”是什么意思,因为这是一个模糊的术语。你是说自动生成步骤、编译步骤、链接步骤等等……出于某种原因,它说protofile1.pb.h找不到,因为protofile2依赖protofile1。