[英]Undefined reference when linking protobuf
我正在Mac上工作。 我用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
位於/usr/local/Cellar/protobuf/2.6.1/lib
。
我編寫了以下虛擬應用程序,希望調用此構造函數 :
// 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
, 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> >*)
為什么我的.dylib
符號列表中的basic_string
名稱空間以::__1
前綴? 我怎樣才能解決這個問題?
如果這不是問題(也許是無法處理的工件),為什么我仍然收到對我知道要定義的構造函數調用的未定義引用?
我正在使用gcc 5.3.0
編譯test.cc
如評論中所述,Homebrew使用clang
而不是g++
構建。
我刪除了brew
安裝的protobuf,簽出並構建了源代碼,將新的.dylib
復制到/usr/local/lib
並運行良好。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.