简体   繁体   English

如何将在依赖CMake项目中创建的Protobuf生成的.h文件正确导入到父CMake项目中?

[英]How do I properly import, into a parent CMake project, a Protobuf-generated .h file created in a dependency CMake project?

Here is what my project hierarchy looks like (simplified): 这是我的项目层次结构(简化):

+-- CMakeLists.txt
+-- party
|   +-- CMakeLists.txt
|   +-- include
|   \-- src
|       \-- party.cc
\-- super
    +-- CMakeLists.txt
    +-- include
    |   \-- super.h
    \-- src
        +-- super.cc
        \-- super.proto

I have a library CMake project, called Super, that has some Google Protobuf code in it. 我有一个名为Super的库CMake项目,其中包含一些Google Protobuf代码。 A source file, super.proto , is used by the protoc compiler to create super.pb.cc and super.pb.h . super.proto编译器使用源文件super.proto来创建super.pb.ccsuper.pb.h Super's CMakeLists.txt looks like this: Super的CMakeLists.txt看起来像这样:

cmake_minimum_required(VERSION 3.1)
project(Super)
find_package(Protobuf REQUIRED)

set(INCLUDE_DIR "include")
set(SRC_DIR "src")

add_definitions("-std=c++11")

include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${INCLUDE_DIR})
include_directories(${PROTOBUF_INCLUDE_DIRS})

# === Super library ===
PROTOBUF_GENERATE_CPP(super_proto_srcs super_proto_incl ${SRC_DIR}/super.proto)
file(GLOB shared_srcs "${SRC_DIR}/*.cc")
add_library(super SHARED ${shared_srcs} ${super_proto_srcs})
target_link_libraries(super ${PROTOBUF_LIBRARIES})

# === Install and exports ===
set(${PROJECT_NAME}_INCLUDE_DIRS
    ${PROJECT_SOURCE_DIR}/include
    CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)

Super is to become a shared library named libsuper.so . Super将成为名为libsuper.so的共享库。 It has a header file that depends on super.pb.h . 它具有一个依赖于super.pb.h的头文件。

Party is a binary that uses libsuper.so . Party是使用libsuper.so的二进制文件。 It doesn't care about Protobuf, at least not directly. 它并不关心Protobuf,至少不是直接关心。 It includes super.h and calls its method. 它包含super.h并调用其方法。 Party has a CMakeLists.txt that looks like this: 派对的CMakeLists.txt如下所示:

cmake_minimum_required(VERSION 3.1)
project(Party)

set(INCLUDE_DIR "include")
set(SRC_DIR "src")

add_definitions("-std=c++11")

include_directories(${INCLUDE_DIR})
include_directories(${Super_INCLUDE_DIRS})

# === Party binary ===
file(GLOB binary_srcs "${SRC_DIR}/*.cc")
add_executable(party ${binary_srcs})
target_link_libraries(party super)

At the level of the parent directory, the unifying CMakeLists.txt looks like this: 在父目录级别,统一的CMakeLists.txt如下所示:

cmake_minimum_required(VERSION 3.1)
project(SuperParty)

add_subdirectory(super)
add_subdirectory(party)

I can build Super on its own with no errors. 我可以自己构建Super,而不会出错。 The Protobuf-generated files are created, also libsuper.so . Protobuf生成的文件也将创建libsuper.so However, when I try to build the master project, SuperParty, I get the following error: 但是,当我尝试构建主项目SuperParty时,出现以下错误:

In file included from superparty/party/src/party.cc:1:0:
superparty/super/include/super.h:1:22: fatal error: super.pb.h: No such file or directory

The file exists in SuperParty's build/ directory, called build/super/super.pb.h . 该文件位于SuperParty的build/目录中,称为build/super/super.pb.h My problem is that I need to tell Party to include this auto-generated file, but I don't know where to get that file's path so that I can create the appropriate line in Party's CMakeLists.txt . 我的问题是我需要告诉Party包含此自动生成的文件,但是我不知道从何处获取该文件的路径,以便可以在Party的CMakeLists.txt创建适当的行。 Party effectively needs a reference to the CMAKE_BINARY_PATH that the master project is using. 参与方实际上需要引用主项目正在使用的CMAKE_BINARY_PATH。

Turns out there's an automatic variable created named Super_BINARY_DIR that I could use in Party's CMakeLists.txt , but there is, in fact, an even better way to do it. 事实证明,可以在Party的CMakeLists.txt使用一个名为Super_BINARY_DIR的自动变量,但实际上,还有一种更好的方法。 Instead of modifying Party, which was already getting its list of includes from Super_INCLUDE_DIRS , I added a CMAKE_CURRENT_BINARY_DIR to the list of includes that Super created for Party to use. 我没有修改Party,后者已经从Super_INCLUDE_DIRS获取了它的包含列表,而是向该Super创建供Party使用的包含列表中添加了一个CMAKE_CURRENT_BINARY_DIR Here is the new ending to Super's CMakeLists.txt : 这是Super的CMakeLists.txt的新结尾:

# === Install and exports ===
set(${PROJECT_NAME}_INCLUDE_DIRS
    ${CMAKE_CURRENT_BINARY_DIR}
    ${PROJECT_SOURCE_DIR}/include
    CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM