Protobuf无法在ubuntu上链接

时间:2015-06-14 09:29:05

标签: 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-dev protobuf-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

1 个答案:

答案 0 :(得分:2)

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

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