【问题标题】:Can't build test C++ app with Google's protobuf无法使用 Google 的 protobuf 构建测试 C++ 应用程序
【发布时间】:2016-08-25 16:13:17
【问题描述】:

我正在尝试使用 CMake 构建示例 C++ protobuf 应用程序。但是链接器找不到一些与protobuf相关的方法。

我使用来自developers guide 的示例 .proto 文件。但是当链接器尝试为 protobuf 生成的 C++ 代码构建目标文件时,我遇到了很多这样的错误:

undefined reference to `google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(int, std::string const&, google::protobuf::io::CodedOutputStream*)'
undefined reference to `google::protobuf::io::CodedOutputStream::WriteStringWithSizeToArray(std::string const&, unsigned char*)'
undefined reference to `google::protobuf::internal::empty_string_'
undefined reference to `google::protobuf::internal::InitEmptyString()'
undefined reference to `google::protobuf::internal::empty_string_once_init_'
undefined reference to `google::protobuf::internal::WireFormat::VerifyUTF8StringFallback(char const*, int, google::protobuf::internal::WireFormat::Operation, char const*)'

我使用自己使用 GCC 4.8.4 构建的 protobuf 2.6.1。示例应用程序由 CMake 在 QTCreator 中使用 QT 5.5.1 工具集和 GCC 4.8.4 构建。有我的 CMakeList.txt

project(protobuf-test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
LINK_DIRECTORIES(/usr/lib)
TARGET_LINK_LIBRARIES(${PROJECT_NAME} pthread protobuf)

感谢您的帮助!

【问题讨论】:

  • 如果你自己构建了protobuf,你是在哪里安装的?在系统/usr/lib?在通常的/usr/local/lib?其他地方?
  • 您应该粘贴链接器命令并确保您的 protobuf 库位于搜索路径中。
  • 我将 libprotobuf 安装到 /usr/lib 中。我在 CMakeList.txt 中使用LINK_DIRECTORIES(/usr/lib) 将此路径添加到链接器搜索路径。
  • /usr/lib 添加到link_directories 是没有意义的,应该自动包含它。对 protobuf 使用 find_file 和 find_libraries 并使用从中获得的变量。

标签: c++ gcc cmake protocol-buffers


【解决方案1】:

问题出在链接器搜索路径中。链接器尝试使用 OS 发行版(版本 8.0.0)中的 libprotobuf,而不是我构建的 lib(版本 9.0.1)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    相关资源
    最近更新 更多