2016-08-02 64 views
0

我正在使用Mac。我用brew install protobuf --c++11安装了libprotobuf。链接protobuf时未定义的引用

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

我写了下面的虚拟应用程序希望调用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); 
} 

当我编译应用程序,我得到一个未定义的引用错误:

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 

当我检查.dylibStringOutputStream,这有点不吉利。

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> >*) 

为什么basic_string通过::__1在我为.dylib符号列表命名空间前缀?我怎样才能解决这个问题?

如果这不是一个问题(也许是一个unmangling工件),为什么我仍然收到一个未定义的引用,我知道要定义的构造函数调用?我使用gcc 5.3.0编译test.cc

+2

不是GCC 5的字符串类型与GCC 4不同吗?您可能需要一个使用GCC 5编译的库。 –

+0

@KerrekSB该库是从source('Built from source ... with:--C++ 11')构建的。你认为完全避免“酿造”是值得的,并试图克隆独立回购并构建它? – erip

+2

brew版本是用clang构建的,默认为-stdlib = libC++。 Gcc默认为-stdlib = libstdC++。两者不兼容。 –

回答

0

正如评论中所提到的,Homebrew正在用clang而不是g++构建。

我删除了由brew安装的protobuf,签出并构建源代码,将新的.dylib复制到/usr/local/lib,它工作正常。

+1

我必须问...为什么不在Mac上构建时使用Xcode的铿锵声? –

+1

@RichardHodges最终它将在Linux上。试图尽可能“黑匣子”,尽可能多地关注“真正的问题”(如链接..):) – erip

+1

@RichardHodges如果没有别的,教学理由对我来说足够好。 – erip

相关问题