1

我正在使用 gcc 运行 ubuntu 16.04。

我的q.ccp文件是

#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;
}

用于编译的命令:

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

protoc --version返回:2.2.0

gcc --版本

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.

编译错误:

/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

但是当我离开这个-D_GLIBCXX_USE_CXX11_ABI=0选项时,我没有得到任何编译错误。但问题是我在不同的系统上运行可执行文件,需要使用此选项编译程序才能运行。

4

1 回答 1

0

如果您的编译仅在 D_GLIBCXX_USE_CXX11_ABI=0 时成功,那么您可能正在使用使用相同或旧 ABI 预编译的共享库 (libprotobuf),例如 gcc 4.8。

我会检查 g++ 版本。它可能 > 5,这将具有更新的 ABI。

于 2017-08-16T02:23:08.497 回答