2016-08-02 357 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