繁体   English   中英

链接protobuf时未定义参考

[英]Undefined reference when linking protobuf

我正在Mac上工作。 我用brew install protobuf --c++11

17:51 $ brew info protobuf
protobuf: stable 2.6.1 (bottled), devel 3.0.0-beta-4, HEAD
Protocol buffers (Google's data interchange format)
https://github.com/google/protobuf/
/usr/local/Cellar/protobuf/2.6.1 (149 files, 7.0M) *
  Built from source on 2016-08-02 at 17:42:15 with: --c++11

libprotobuf.dylib位于/usr/local/Cellar/protobuf/2.6.1/lib

我编写了以下虚拟应用程序,希望调用此构造函数

// test.cc
#include <string>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>

int main() {
  std::string s{"hello"};
  google::protobuf::io::StringOutputStream sos(&s);
}

编译应用程序时,出现未定义的引用错误:

17:55 $ g++ -L/usr/local/Cellar/protobuf/2.6.1/lib -std=c++14 test.cc -lprotobuf
Undefined symbols for architecture x86_64:
  "google::protobuf::io::StringOutputStream::StringOutputStream(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)", referenced from:
      _main in ccyQlDM5.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

当我检查.dylibStringOutputStreamStringOutputStream

17:56 $ nm /usr/local/Cellar/protobuf/2.6.1/lib/libprotobuf.dylib | c++filt | grep "StringOutputStream(std::"
000000000000e3ac T google::protobuf::io::StringOutputStream::StringOutputStream(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)
000000000000e398 T google::protobuf::io::StringOutputStream::StringOutputStream(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*)

为什么我的.dylib符号列表中的basic_string名称空间以::__1前缀? 我怎样才能解决这个问题?

如果这不是问题(也许是无法处理的工件),为什么我仍然收到对我知道要定义的构造函数调用的未定义引用?

我正在使用gcc 5.3.0编译test.cc

如评论中所述,Homebrew使用clang而不是g++构建。

我删除了brew安装的protobuf,签出并构建了源代码,将新的.dylib复制到/usr/local/lib并运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM