问题描述
我正在尝试使用 cmake 使用协议缓冲区构建我的小项目.
I'm trying to use cmake to build my little project using protocol buffers.
有一个根目录,其中包含许多子目录,其中包含许多库和可执行文件.我的第一个想法是将我的 .proto 文件放在一个子目录中,但是当我阅读 this answer 时,我做了一个库取而代之.但是当我尝试在我的可执行文件中包含一个消息头时,它找不到它.
There's a root directory with a number of subdirectories with a number of libraries and executables. My first thought was to have my .proto-files in a subdirectory, but when I read this answer I made a library out of it instead. But when I try to include a messages header in my executable it can't find it.
错误信息:
fatal error: msgs.pb.h: No such file or directory
#include "msgs.pb.h"
^
compilation terminated.
我通过创建一个目录build"然后从里面创建cmake .. && make"来运行它.我看过了,似乎生成的文件被放入了 build/messages,所以我可以做 include_directories(build/messages) 但这似乎不......是否有使用 protobuf 执行此操作的正确方法?我希望将消息文件放在它们自己的文件夹中的原因是它们将用于许多不同的小型可执行文件.
I'm running it by creating a dir "build" and then "cmake .. && make" from inside it. I've looked and it seems the generated files get put in build/messages, so I could do include_directories(build/messages) but that doesn't seem...proper. Is there a proper way of doing this with protobuf? The reason I want the messages file in their own folder is they they'll be used in a lot of different small executables.
还感谢任何其他改进我的 CMake 结构的一般提示:)
Any other general tips for improvements to my CMake-structure is also appreciated :)
目录:
root
messages
core
server
root/CMakeLists.txt:
root/CMakeLists.txt:
project(lillebror)
cmake_minimum_required(VERSION 2.8)
cmake_policy(SET CMP0015 NEW)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost COMPONENTS date_time log thread system)
find_package(Protobuf REQUIRED)
if(Boost_FOUND)
add_definitions(-std=c++11)
add_subdirectory(messages)
add_subdirectory(core)
add_subdirectory(server)
add_subdirectory(testserver)
endif()
messages/CMakeLists.txt:
messages/CMakeLists.txt:
file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
add_library(messages STATIC ${ProtoSources} ${ProtoHeaders})
target_link_libraries(messages ${Boost_LIBRARIES} ${PROTOBUF_LIBRARY})
核心/CMakeLists.txt:
core/CMakeLists.txt:
aux_source_directory(src SRC_LIST)
add_library(core STATIC ${SRC_LIST})
target_link_libraries(core messages ${Boost_LIBRARIES})
服务器/CMakeLists.txt:
server/CMakeLists.txt:
aux_source_directory(src SRC_LIST)
include_directories(../messages) <---- I thought this would sove my problem
include_directories(../core/src)
link_directories(../core/build)
add_executable(server ${SRC_LIST})
target_link_libraries(server core ${Boost_LIBRARIES})
服务器/main.cpp:
server/main.cpp:
#include "msgs.pb.h"
int main()
{
return 0;
}
推荐答案
我认为这里的问题在于 PROTOBUF_GENERATE_CPP
函数将 .pb.h 和 .pb.cc 文件设置为存在于构建树中,而不是源树中.
I think the problem here is that the PROTOBUF_GENERATE_CPP
function sets up the .pb.h and .pb.cc files to exist in the build tree, not in the source tree.
这是一个很好的做法(不会污染源代码树),但这意味着您的调用 include_directories(../messages)
向搜索路径添加了错误的值.这是添加源目录root/messages",而您想要[build root]/messages".
This is good practice (not polluting the source tree), but it means that your call include_directories(../messages)
is adding the wrong value to the search paths. This is adding the source directory "root/messages", whereas you want "[build root]/messages".
您可能只需将该行替换为:
You could probably just replace that line with:
include_directories(${CMAKE_BINARY_DIR}/messages)
然而,更健壮、可维护的方法可能是在消息/CMakeLists.txt 中设置所需的包含路径.要将这个值暴露给父作用域,这需要使用 set(... PARENT_SCOPE)
或:
However, a more robust, maintainable way might be to set the required include path inside the messages/CMakeLists.txt. To expose this value to the parent scope, this would need to either use set(... PARENT_SCOPE)
or:
set(ProtobufIncludePath ${CMAKE_CURRENT_BINARY_DIR}
CACHE INTERNAL "Path to generated protobuf files.")
然后在顶层 CMakeLists.txt 中,您可以:
Then in the top-level CMakeLists.txt, you can do:
include_directories(${ProtobufIncludePath})
如果你的 messages
库本身需要#include 生成的 protobuf 文件(这很正常),那么它也应该有一个类似的 include_directories
调用.
If your messages
library itself needs to #include the generated protobuf files (this would be normal), then it too should have a similar include_directories
call.
说了这么多,如果你可以指定 CMake v2.8.12 作为最低要求,你可以使用 target_include_directories
命令.
Having said all that, if you can specify CMake v2.8.12 as the minimum, you can use the target_include_directories
command instead.
在 add_library
调用之后的消息/CMakeLists.txt 中,您只需执行以下操作:
In messages/CMakeLists.txt after the add_library
call, you'd simply do:
target_include_directories(messages PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
然后任何其他依赖于 messages
的目标会自动将适当的消息"包含目录添加到自己的目录中 - 您根本不需要显式调用 include_directories
.
Then any other target which depends on messages
automatically has the appropriate "messages" include dirs added to its own - you don't need to explicitly call include_directories
at all.
这篇关于使用 Google 协议缓冲区的 CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!