简体   繁体   中英

Undefined reference when linking protobuf

I'm working on a Mac. I installed libprotobuf with 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 .

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.

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 ? 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 .

As referenced in the comments, Homebrew was building with clang instead of 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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