Tobi
Tobi

Reputation: 540

Protobuf cannot be linked on ubuntu

I try to use protobuf but somehow the linking fails (here just snippet):

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 finds the shared object file of protobuf and uses it during linking:

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

and here is a smaller version of my 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})

At first I installed the library via source (was not working either), removed it again and installed the ubuntu package libprobofuv-dev and protobuf-compiler. The output of dpkg -L libprotobuf-dev is here:

> 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

Upvotes: 3

Views: 4222

Answers (1)

Tobi
Tobi

Reputation: 540

As @frymode pointet out in his comment, linkage to NewGeneratedMessageReflection means that the compiler generated code that uses Protobuf version 3 (as I used that version in my .proto files). However, the library files installed from the ubuntu package pulled version 2 onto my system, that's why the methods could not be found.

Solution was to remove everything again and build protobuf including protoc from source.

Upvotes: 2

Related Questions