简体   繁体   English

链接protobuf时未定义参考

[英]Undefined reference when linking protobuf

I'm working on a Mac. 我正在Mac上工作。 I installed libprotobuf with brew install protobuf --c++11 . 我用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 lives in /usr/local/Cellar/protobuf/2.6.1/lib . libprotobuf.dylib位于/usr/local/Cellar/protobuf/2.6.1/lib

I wrote the following dummy app hoping to invoke this constructor : 我编写了以下虚拟应用程序,希望调用此构造函数

// 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);
}

When I compile the app, I get an undefined reference error: 编译应用程序时,出现未定义的引用错误:

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

When I check the .dylib for StringOutputStream , it's a little wonky. 当我检查.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> >*)

Why is basic_string namespace-prefixed by ::__1 in my list of symbols for that .dylib ? 为什么我的.dylib符号列表中的basic_string名称空间以::__1前缀? How can I fix this? 我怎样才能解决这个问题?

If this isn't a problem (perhaps an artifact of unmangling), why am I still receiving an undefined reference to a constructor call I know to be defined? 如果这不是问题(也许是无法处理的工件),为什么我仍然收到对我知道要定义的构造函数调用的未定义引用?

I'm using gcc 5.3.0 to compile test.cc . 我正在使用gcc 5.3.0编译test.cc

As referenced in the comments, Homebrew was building with clang instead of g++ . 如评论中所述,Homebrew使用clang而不是g++构建。

I removed the protobuf installed by brew , checked out and built the source, copied the new .dylib to /usr/local/lib and it worked fine. 我删除了brew安装的protobuf,签出并构建了源代码,将新的.dylib复制到/usr/local/lib并运行良好。

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

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