我正在尝试使用 CMake 和 Protocol Buffers 来构建我的小项目。
根目录下有许多子目录,包含很多库和可执行文件。我的第一想法是将 .proto 文件放在一个子目录中,但当我阅读了 这个答案 后,我将其改为了库。但是当我试图在我的可执行文件中包含消息头时,却找不到它。
错误信息:
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;
}