1

I am running ubuntu 16.04 with gcc.

My q.ccp file is

#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/repeated_field.h>
#include <google/protobuf/extension_set.h>
#include <google/protobuf/generated_message_reflection.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/descriptor.h>

int main(int argc, char **argv)
{

    google::protobuf::Message* msg;


    const google::protobuf::Descriptor* message_desc = msg->GetDescriptor();


    const google::protobuf::FieldDescriptor * fd_name = message_desc->FindFieldByName("name");


    return 0;
}

command used for compilation:

g++ -D_GLIBCXX_USE_CXX11_ABI=0 q.cpp -L/usr/lib/x86_64-linux-gnu /usr/local/lib/libprotobuf.a -lpthread

protoc --version returns: 2.2.0

gcc --version

gcc (Ubuntu 4.8.5-4ubuntu2) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

compile error:

/tmp/cciGJPqq.o: In function `main':
q.cpp:(.text+0x5f): undefined reference to `google::protobuf::Descriptor::FindFieldByName(std::string const&) const'
collect2: error: ld returned 1 exit status

But when I leave the -D_GLIBCXX_USE_CXX11_ABI=0 option, I get no compile error. But the problem is that I running the executable on different system which require compilation with this option for the program to run.

v78
  • 2,803
  • 21
  • 44
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Danh Dec 22 '16 at 09:56
  • @Danh, c'mon man, read the full question at least. Are u really serious? – v78 Dec 22 '16 at 09:59
  • I won't change my decision, the fact is g++ had made an ABI change, your programme needs to be compiled with the same ABI with your library – Danh Dec 22 '16 at 10:02
  • 1
    [This answer](http://stackoverflow.com/a/32500124/4115625) in the linked question had indicated that matter. – Danh Dec 22 '16 at 10:03
  • 1
    your protobuf is installed in `/usr/local`, it seems like that it's compiled and installed manually, you need to compile both of them with the same ABI – Danh Dec 22 '16 at 10:05
  • thanks @Danh, I believe your last point might solve my problem. – v78 Dec 22 '16 at 10:10

1 Answers1

0

If your compilation succeeds only with D_GLIBCXX_USE_CXX11_ABI=0, then you may be using a shared library (libprotobuf) that is precompiled using the same or using an older ABI, like gcc 4.8.

I would check the g++ version. It may be > 5, which would have the newer ABI.

r11
  • 107
  • 1
  • 4