我在两个不同的文件夹中有两个Proto文件,并且正在尝试使用CMake构建整个项目。
protobuf_generate_cpp
生成的依赖项。但是对于生成protofile1,我具有protofile2,因为它具有依赖性。如何使用CMake做到这一点?
如何编译原型文件,并使用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?
protobuf_generate_cpp
命令未定义目标,但它定义了CMake 变量,它们引用了自动生成的源文件(protobuf_generate_cpp
,.cc
)。这些变量应用于通过.h
定义目标。您的位置正确,但是folder2中的CMakeLists.txt文件也应调用add_library()
来处理protobuf_generate_cpp
。
[此外,如果您同时使用CMake来构建这两者,则顶级CMake(在protofile2.proto
和folder1
的父文件夹中)可以找到Protobuf,因此您无需两次查找。这样的事情应该起作用:
CMakeLists.txt(顶级):
folder2
folder2 / CMakeLists.txt
cmake_minimum_required(VERSION 3.3)
find_package(Protobuf REQUIRED)
add_subdirectory(folder2)
add_subdirectory(folder1)
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})