我正在使用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
当我检查.dylib
为StringOutputStream
,这有点不吉利。
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
。
不是GCC 5的字符串类型与GCC 4不同吗?您可能需要一个使用GCC 5编译的库。 –
@KerrekSB该库是从source('Built from source ... with:--C++ 11')构建的。你认为完全避免“酿造”是值得的,并试图克隆独立回购并构建它? – erip
brew版本是用clang构建的,默认为-stdlib = libC++。 Gcc默认为-stdlib = libstdC++。两者不兼容。 –