【发布时间】:2014-01-16 10:23:17
【问题描述】:
我正在尝试使用 cmake 使用协议缓冲区构建我的小项目。
有一个根目录,其中包含许多子目录以及许多库和可执行文件。我的第一个想法是将我的 .proto 文件放在子目录中,但是当我阅读 this answer 时,我用它创建了一个库。但是当我尝试在我的可执行文件中包含消息头时,它找不到它。
错误信息:
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做这件事的正确方法?我希望将消息文件放在它们自己的文件夹中的原因是它们将被用于许多不同的小型可执行文件中。
对于改进我的 CMake 结构的任何其他一般提示也表示赞赏:)
目录:
root
messages
core
server
根目录/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()
消息/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:
aux_source_directory(src SRC_LIST)
add_library(core STATIC ${SRC_LIST})
target_link_libraries(core messages ${Boost_LIBRARIES})
服务器/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:
#include "msgs.pb.h"
int main()
{
return 0;
}
【问题讨论】:
标签: c++ cmake protocol-buffers