无法使用谷歌的protobuf构建测试C++应用程序

Can't build test C++ app with Google's protobuf

本文关键字:构建 测试 C++ 应用程序 protobuf 谷歌      更新时间:2023-10-16

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

我使用开发人员指南中的示例。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)

谢谢你的帮助!

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