Protobuf:无法在 message.pb.h 文件中打开包含文件 common.h

Protobuf: Cannot open include file common.h in message.pb.h file

我正在尝试编译这个项目:https://github.com/shaochuan/cmake-protobuf-example。我用 msvc2015 (x64) 成功编译了 protobuf。检查项目运行没有错误(所有结果:通过)。

我在cmake中设置项目,提供所有路径:

Protobuf_INCLUDE_DIR=C:/TK/protobuf-330/msvc2015_64/install/include Protobuf_LIBRARY_DEBUG=C:/TK/protobuf-330/msvc2015_64/install/lib/libprotobufd.lib Protobuf_LITE_LIBRARY_DEBUG=C:/TK/protobuf-330/msvc2015_64/install/lib/libprotobuf-lited.lib

Protobuf_PROTOC_EXECUTABLE=C:/TK/protobuf-330/msvc2015_64/install/bin/protoc.exe Protobuf_PROTOC_LIBRARY_DEBUG=C:/TK/protobuf-330/msvc2015_64/install/lib/libprotocd.lib Protobuf_SRC_ROOT_FOLDER=C:\TK\src\protobuf-330\src

原型项目编译没有任何错误。它包括 message.ccmessage.h 文件。 CMakeLists.txt 看起来像这样:

INCLUDE(FindProtobuf)
FIND_PACKAGE(Protobuf REQUIRED)
INCLUDE_DIRECTORIES(${PROTOBUF_INCLUDE_DIR})
PROTOBUF_GENERATE_CPP(PROTO_SRC PROTO_HEADER message.proto)
ADD_LIBRARY(proto ${PROTO_HEADER} ${PROTO_SRC})

如果我用下面的CMakeLists.txt编译主项目:

PROJECT(rpc)
CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
ADD_SUBDIRECTORY(proto)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_BINARY_DIR})
ADD_EXECUTABLE(main main.cpp)
TARGET_LINK_LIBRARIES(main proto ${PROTOBUF_LIBRARY})

然后构建它,我收到此错误 (C1083) 说:

"Cannot open include file: 'google/protobuf/stubs/common.h': No such file or directory" c:\tktest\protobuftest\src\proto\message.pb.h

文件中的错误行是:

#include <google/protobuf/stubs/common.h>

我无法弄清楚发生了什么 wrong.Any 感谢提示。关于 cmake + protobuf + visual studio 2015 是否存在一些已知问题? 提前致谢。

我将在这里进行有根据的猜测,因为我对 protobuf 一无所知。

更喜欢 target_* 命令。

原型项目CMakeLists.txt:

target_include_directories(proto
  PUBLIC
    "${Protobuf_INCLUDE_DIRS}")

也就是说,所有使用 proto 目标的东西都可以访问“${Protobuf_INCLUDE_DIRS}”中的任何包含,proto 本身也可以访问它们.

在我的 PC 上,项目正在编译,但未 linking。因此,要解决此问题,proto 库需要 link protobuf 库。这可以通过将以下行添加到 proto/CMakeLists.txt

来完成
TARGET_LINK_LIBRARIES(proto ${PROTOBUF_LIBRARY})

此更改后它可以在我的 PC 上运行。

在您的 CMakeLists.txt 中,您应该将 PROTOBUF_INCLUDE_DIR 替换为 Protobuf_INCLUDE_DIRProtobuf_INCLUDE_DIRS(注意末尾的 S)和 PROTOBUF_LIBRARY Protobuf_LIBRARYProtobuf_LIBRARIES。 CMake 变量名称区分大小写。我刚刚检查了 CMake 3.9 版本。使用 find_package(Protobuf) 时隐式调用的 FindProtobuf.cmake 模块在 运行 成功时定义了这些变量。