c++ - ubuntu上无法链接protobuf

标签 c++ ubuntu linker cmake protocol-buffers

我尝试使用 protobuf 但不知何故链接失败(这里只是片段):

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

最佳答案

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

解决方案是再次删除所有内容并构建 protobuf,包括来自源的协议(protocol)。

关于c++ - ubuntu上无法链接protobuf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30827824/

相关文章:

c++ - 链接到 visual studio 2013 boost

c++ - 我怎样才能使 Qt 应用程序更小

ubuntu - Postfix-Mailman "Recipient address rejected: User unknown in local recipient table"

c++ - 在成员函数中创建对象

python - 如何在 Ubuntu 16.04 上安装 python-gasp?

Apache + WSGI : Address already in use: make_sock: could not bind to address

c++ - 使用-static 时如何使gcc/ld 遍历多个 '-l library'?

android - std::map 链接器错误 ndk r8c with APP_STL := gnuSTL_static

c++ - Clang5.0 : previous declaration in "__hash_table"

c++ - C++ 20 中的必需指示符