我正在使用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++。兩者不兼容。 –