簡體   English   中英

鏈接protobuf時未定義參考

[英]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

當我檢查.dylibStringOutputStreamStringOutputStream

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM