【问题标题】:Protobuf cannot be linked on ubuntu无法在 ubuntu 上链接 Protobuf
【发布时间】:2015-06-14 09:29:04
【问题描述】:

我尝试使用 protobuf,但不知何故链接失败(这里只是 sn-p):

Linking CXX executable app
CMakeFiles/app.dir/msg.pb.cc.o: In function `evoswarm::protobuf_AssignDesc_a_5fto_5fb_2eproto()':
msg.pb.cc:(.text+0x133): undefined reference to `google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(google::protobuf::Descriptor const*, google::protobuf::Message const*, int const*, int, int, int, int, int, int)'
msg.pb.cc:(.text+0x190): undefined reference to `google::protobuf::internal::GeneratedMessageReflection::NewGeneratedMessageReflection(google::protobuf::Descriptor const*, google::protobuf::Message const*, int const*, int, int, int, int, int, int)'

Cmake 找到 protobuf 的共享对象文件,并在链接时使用:

/usr/bin/c++    -std=c++11     CMakeFiles/app.dir/main.cpp.o  CMakeFiles/app.dir/msg.pb.cc.o  -o app -rdynamic -lprotobuf -lpthread

这是我的 CMakeLists.txt 的较小版本

cmake_minimum_required (VERSION 2.8)
project(app)

# build ProtoBufs
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIRS})
file(GLOB ProtoFiles "${CMAKE_CURRENT_SOURCE_DIR}/messages/*.proto")
PROTOBUF_GENERATE_CPP(ProtoSources ProtoHeaders ${ProtoFiles})
include_directories(${CMAKE_CURRENT_BINARY_DIR}) # that's where the generated stuff ends

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 ")

#Files for main binary
set(SRC main.cpp)

add_executable (app  ${SRC} ${ProtoSources} ${ProtoHeaders})
TARGET_LINK_LIBRARIES (app ${PROTOBUF_LIBRARIES})

一开始我通过源码安装了库(也没有工作),再次删除它并安装了ubuntu包libprobofuv-devprotobuf-compiler。 dpkg -L libprotobuf-dev 的输出在这里:

> sudo dpkg -L libprotobuf-dev
/.
/usr
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/protobuf-lite.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/protobuf.pc
/usr/lib/x86_64-linux-gnu/libprotobuf-lite.a
/usr/lib/x86_64-linux-gnu/libprotobuf.a
/usr/include
/usr/include/google
/usr/include/google/protobuf
/usr/include/google/protobuf/dynamic_message.h
/usr/include/google/protobuf/generated_enum_reflection.h
/usr/include/google/protobuf/generated_message_reflection.h
/usr/include/google/protobuf/wire_format.h
/usr/include/google/protobuf/service.h
/usr/include/google/protobuf/io
/usr/include/google/protobuf/io/printer.h
/usr/include/google/protobuf/io/coded_stream.h
/usr/include/google/protobuf/io/tokenizer.h
/usr/include/google/protobuf/io/zero_copy_stream_impl_lite.h
/usr/include/google/protobuf/io/zero_copy_stream.h
/usr/include/google/protobuf/io/gzip_stream.h
/usr/include/google/protobuf/io/zero_copy_stream_impl.h
/usr/include/google/protobuf/reflection_ops.h
/usr/include/google/protobuf/extension_set.h
/usr/include/google/protobuf/descriptor.h
/usr/include/google/protobuf/generated_message_util.h
/usr/include/google/protobuf/wire_format_lite_inl.h
/usr/include/google/protobuf/stubs
/usr/include/google/protobuf/stubs/atomicops_internals_pnacl.h
/usr/include/google/protobuf/stubs/type_traits.h
/usr/include/google/protobuf/stubs/atomicops_internals_x86_msvc.h
/usr/include/google/protobuf/stubs/atomicops_internals_arm_gcc.h
/usr/include/google/protobuf/stubs/platform_macros.h
/usr/include/google/protobuf/stubs/once.h
/usr/include/google/protobuf/stubs/atomicops_internals_x86_gcc.h
/usr/include/google/protobuf/stubs/atomicops.h
/usr/include/google/protobuf/stubs/atomicops_internals_generic_gcc.h
/usr/include/google/protobuf/stubs/atomicops_internals_atomicword_compat.h
/usr/include/google/protobuf/stubs/common.h
/usr/include/google/protobuf/stubs/atomicops_internals_arm_qnx.h
/usr/include/google/protobuf/stubs/atomicops_internals_mips_gcc.h
/usr/include/google/protobuf/stubs/template_util.h
/usr/include/google/protobuf/stubs/atomicops_internals_macosx.h
/usr/include/google/protobuf/message_lite.h
/usr/include/google/protobuf/text_format.h
/usr/include/google/protobuf/descriptor_database.h
/usr/include/google/protobuf/descriptor.proto
/usr/include/google/protobuf/message.h
/usr/include/google/protobuf/repeated_field.h
/usr/include/google/protobuf/wire_format_lite.h
/usr/include/google/protobuf/unknown_field_set.h
/usr/include/google/protobuf/descriptor.pb.h
/usr/share
/usr/share/doc
/usr/lib/x86_64-linux-gnu/libprotobuf-lite.so
/usr/lib/x86_64-linux-gnu/libprotobuf.so
/usr/share/doc/libprotobuf-dev

【问题讨论】:

  • NewGeneratedMessageReflection 是版本 3 api,但稳定版本是 2,我认为应该由包管理器安装。你有从最新源构建的 protobuf 吗?看起来它选择了它的编译器和头文件,但 v2 库。
  • @frymode 谢谢!这就是问题所在。我扔掉了所有东西,并从最新的 protobuf 源构建。像魅力一样工作。

标签: c++ ubuntu linker cmake protocol-buffers


【解决方案1】:

正如@frymode 在他的评论中指出的那样,与 NewGeneratedMessageReflection 的链接意味着编译器生成的代码使用 Protobuf 版本 3(正如我在 .proto 文件中使用的版本一样)。但是,从 ubuntu 包安装的库文件将版本 2 拉到了我的系统上,这就是找不到方法的原因。

解决方案是再次删除所有内容并从源代码构建 protobuf,包括 protoc。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-25
    • 1970-01-01
    • 2013-02-15
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多