未定义的引用google :: protobuf :: internal :: empty_string_ [abi:cxx11]

时间:2015-05-08 12:46:43

标签: c++ gcc protocol-buffers

我正在尝试使用Protocol Buffers 2.6.1和GNU GCC 5.1.0(在Ubuntu 14.10上)构建简单的测试应用程序,并且出现以下错误:

stream_iterator

/ home / ragnar / cpp-tools / libs / linux64 / protobuf / lib包含以下库:

/home/ragnar/cpp-tools/gcc-linux/bin/g++   -c  "/home/ragnar/cpp-projects/gprotobuf_test/main.cpp" -g -O0 -Wall   -o ./Debug/main.cpp.o -I. -I/home/ragnar/cpp-tools/libs/linux64/protobuf/include -I.
/home/ragnar/cpp-tools/gcc-linux/bin/g++   -c  "/home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc" -g -O0 -Wall   -o ./Debug/messages.pb.cc.o -I. -I/home/ragnar/cpp-tools/libs/linux64/protobuf/include -I.  
/home/ragnar/cpp-tools/gcc-linux/bin/g++  -o ./Debug/gprotobuf_test @"gprotobuf_test.txt" -L. -L/home/ragnar/cpp-tools/libs/linux64/protobuf/lib  -lprotobuf  
./Debug/main.cpp.o: In function google::protobuf::internal::GetEmptyStringAlreadyInited[abi:cxx11]():  
  /home/ragnar/cpp-tools/libs/linux64/protobuf/include/google/protobuf/generated_message_util.h:80: 
    undefined reference to google::protobuf::internal::empty_string_[abi:cxx11]  
  /home/ragnar/cpp-tools/libs/linux64/protobuf/include/google/protobuf/generated_message_util.h:81: 
    undefined reference to google::protobuf::internal::empty_string_[abi:cxx11]  
./Debug/messages.pb.cc.o: In function protobuf_AssignDesc_messages_2eproto():  
  /home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc:32: 
    undefined reference to google::protobuf::DescriptorPool::FindFileByName(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const  
./Debug/messages.pb.cc.o: In function protobuf_AddDesc_messages_2eproto():  
  /home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc:83: 
    undefined reference to google::protobuf::MessageFactory::InternalRegisterGeneratedFile(char const*, void (*)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&))  
./Debug/messages.pb.cc.o: In function my_message::MergePartialFromCodedStream(google::protobuf::io::CodedInputStream*):
  /home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc:187: 
    undefined reference to google::protobuf::internal::WireFormatLite::ReadString(google::protobuf::io::CodedInputStream*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)  
./Debug/messages.pb.cc.o: In function my_message::SerializeWithCachedSizes(google::protobuf::io::CodedOutputStream*) const:
  /home/ragnar/cpp-projects/gprotobuf_test/messages.pb.cc:247: 
    undefined reference to google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::io::CodedOutputStream*)  
./Debug/messages.pb.cc.o: In function google::protobuf::internal::WireFormatLite::WriteStringToArray(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*):
  /home/ragnar/cpp-tools/libs/linux64/protobuf/include/google/protobuf/wire_format_lite_inl.h:749: 
    undefined reference to google::protobuf::io::CodedOutputStream::WriteStringWithSizeToArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)  
./Debug/messages.pb.cc.o:(.rodata._ZTV10my_message[_ZTV10my_message]+0x20): 
    undefined reference to google::protobuf::Message::GetTypeName[abi:cxx11]() const  
 ./Debug/messages.pb.cc.o:(.rodata._ZTV10my_message[_ZTV10my_message]+0x40): 
    undefined reference to google::protobuf::Message::InitializationErrorString[abi:cxx11]() const  
collect2: error: ld returned 1 exit status  
gprotobuf_test.mk:93: recipe for target "Debug/gprotobuf_test" failed  
make[1]: *** [Debug/gprotobuf_test] Error 1  
make[1]: Leaving directory "/home/ragnar/cpp-projects/gprotobuf_test"  
Makefile:4: recipe for target "All" failed  
make: *** [All] Error 2  

这是简单的messages.proto文件:

libprotobuf.a  
libprotobuf.so.9.0.1  
libprotobuf-lite.a  
libprotobuf-lite.so.9.0.1  
libprotoc.a  
libprotoc.so.9.0.1  

和我正在努力工作的代码:

option java_package = "my.package";

message my_message {
  required string word = 1;
  required uint32 number = 2;
}

Gcc是从源代码构建的,具有以下选项:

#include <iostream>
#include <vector>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include "messages.pb.h"

std::vector<unsigned char> encode( google::protobuf::Message & msg )
{
  std::vector<unsigned char> data( msg.ByteSize() + 
    google::protobuf::io::CodedOutputStream::VarintSize32( msg.ByteSize() ) );
  google::protobuf::io::ArrayOutputStream array_out( &data[0], data.size() );
  google::protobuf::io::CodedOutputStream coded_out( &array_out );
  coded_out.WriteVarint32( msg.ByteSize() );
  msg.SerializeToCodedStream( &coded_out );
  return data;
}

void decode( const std::vector<unsigned char> & data, google::protobuf::Message & msg )
{
  google::protobuf::io::ArrayInputStream array_in( &data[0] , data.size() );
  google::protobuf::io::CodedInputStream coded_in( &array_in );
  google::protobuf::uint32 size;
  coded_in.ReadVarint32( &size );
  google::protobuf::io::CodedInputStream::Limit msg_limit = coded_in.PushLimit( size );
  msg.ParseFromCodedStream( &coded_in );
  coded_in.PopLimit( msg_limit );
}

int main(int argc, char **argv)
{
  my_message in_msg;
  in_msg.set_word( \"blah blah\" );
  in_msg.set_number( 123 );
  std::vector<unsigned char> data = encode( in_msg );

  my_message out_msg;
  decode( data, out_msg );
  std::cout << \"word: \" << out_msg.word() << \" number: \" << out_msg.number() << std::endl;
  return 0;
}

和Protobuf是从源代码构建的,具有以下选项:

--enable-64bit --enable-32bit --enable-languages=c,c++ --enable-multilib
--disable-nls --enable-threads=posix --enable-checking=release
--enable-lto --enable-multiarch --with-multilib-list=m32,m64,mx32 
--with-tune=generic --enable-shared --with-glibc-version=2.13
--enable-libstdcxx-time=rt

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:30)

我怀疑这是一个C ++ ABI问题。 std::string的ABI在GCC 5中发生了变化(与C ++ 11要求相关,但即使您不使用C ++ 11也适用)。参见:

https://gcc.gnu.org/gcc-5/changes.html#libstdcxx

如果libprotobuf是使用GCC 4.x或之前构建的,但您的应用程序是使用GCC 5构建的,那么您将看到问题,因为libprotobuf在其界面中使用std::string。您有两种选择:

  1. 使用GCC 5重建libprotobuf(但现在使用GCC 4构建的任何应用程序都无法与新版本的libprotobuf一起使用)。
  2. 按照上述链接所述,使用-D_GLIBCXX_USE_CXX11_ABI=0构建应用。这将迫使GCC使用旧的ABI版本。

答案 1 :(得分:8)

我有类似的问题

writeProto.cpp:(.text+0x2a8): undefined reference to `google::protobuf::internal::VerifyVersion(int, int, char const*)'
writeProto.cpp:(.text+0x308): undefined reference to `tutorial::AddressBook::AddressBook()'
writeProto.cpp:(.text+0x3a2): undefined reference to `google::protobuf::Message::ParseFromIstream(std::istream*)'
writeProto.cpp:(.text+0x463): undefined reference to `google::protobuf::Message::SerializeToOstream(std::ostream*) const'
writeProto.cpp:(.text+0x4b4): undefined reference to `google::protobuf::ShutdownProtobufLibrary()'
writeProto.cpp:(.text+0x4c8): undefined reference to `tutorial::AddressBook::~AddressBook()'
writeProto.cpp:(.text+0x515): undefined reference to `tutorial::AddressBook::~AddressBook()'

我在命令行上写了这个,现在我的代码正在运行。

c++ writeProto.cpp addressbook.pb.cc `pkg-config --cflags --libs protobuf`

答案 2 :(得分:1)

我有一个类似的问题,这是因为我用GNU的libstdc++编译了protobuf库,但是在应用程序中我使用的是Clang的libc++,而且这两个都不起作用在一起。

一旦我使用ldd <file>查看文件所依赖的共享对象,一切都变得清晰了。