[英]Can't compile example from google protocol buffers
我搜索其他主題,但它們對我沒有幫助 =(。在我的工作服務器上,我沒有 sudo 權限,所以我安裝了 PB
./configure --prefix=/home/username/local
然后我用“person”示例創建源文件並成功地用 protoc 編譯它。
我沒有 pkg-info =(。我試着用
g++ -I /home/username/local/include -L /home/username/local/lib -lprotobuf -lpthread main.cpp person.pb.cc
然后有十億個模擬錯誤,即
person.pb.cc:(.text+0x4cf):對`google::protobuf::internal::kEmptyString'的未定義引用
我認為,這是鏈接問題,但如何解決?
echo $LD_LIBRARY_PATH /home/username/local/lib
在 main.cpp 中:
#include "person.pb.h"
...
謝謝。
將庫放在最后:
g++ -I /home/username/local/include -L /home/username/local/lib main.cpp person.pb.cc -lprotobuf -pthread
從GCC 鏈接選項:
-llibrary -l library Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.) It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, `foo.o -lz bar.o' searches library `z' after file foo.o but before bar.o. If bar.o refers to functions in `z', those functions may not be loaded.
此外,使用-pthread
而不是-lpthread
因為-pthread
可能會為預處理器和鏈接器設置標志。
庫鏈接標志位於編譯器參數的末尾:
g++ -I /home/username/local/include -L /home/username/local/lib main.cpp person.pb.cc -lprotobuf -lpthread
還有一個非常普遍的問題,不僅適用於鏈接 protobuf,也適用於其他庫。 由於這是我的問題,因此我會將解決方案放在這里,也許有人會覺得它有用。
確保您嘗試鏈接到具有相同 ABI 的庫。 C++ ABI 在 GCC 4.7.0 中發生了變化。 因此,您不能將使用 GCC<4.7.0 編譯的庫(如果您像我的情況一樣從包存儲庫中獲取包,則可能是 protobuf)與使用 GCC>4.7.0 編譯的庫鏈接。
這個問題可以通過讓植物鏈接抱怨 std::basic_string 和 std::list 來識別,因為這是 C++ 中最突出的 ABI 變化。 我的鏈接器尖叫看起來像這樣:
/bincrafters/stable/package/c0c1ef10e3d0ded44179e28b669d6aed0277ca6a/lib -L/home/adam/.conan/data/libpcap/1.8.1/bincrafters/stable/package/0a813c597d519ec14c71192b99d7de0a92bbc1c3/lib -L/home/adam/.conan/data/zmq/4.2.2/bincrafters/stable/package/0144a3b0aceb8edb5e63295c432a8de8020ab1b7/lib -L/home/adam/.conan/data/libsodium/1.0.16/bincrafters/stable/package/db2ca884c9793e0b0fb54ec3f846326d1addacc8/lib -Wl,-rpath,/hoar, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::io::CodedOutputStream*)'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o: In function `tutorial::Person_PhoneNumber::InternalSerializeWithCachedSizesToArray(unsigned char*) const':
/home/adam/.conan/data/protobuf/3.9.1/bincrafters/stable/package/053ea29eb0edc6b1695c893b738a971110c756fd/include/google/protobuf/wire_format_lite.h:1673: undefined reference to `google::protobuf::io::CodedOutputStream::WriteStringWithSizeToArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)'
/home/adam/.conan/data/protobuf/3.9.1/bincrafters/stable/package/053ea29eb0edc6b1695c893b738a971110c756fd/include/google/protobuf/wire_format_lite.h:1673: undefined reference to `google::protobuf::io::CodedOutputStream::WriteStringWithSizeToArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o: In function `google::protobuf::internal::GetEmptyStringAlreadyInited[abi:cxx11]()':
/home/adam/.conan/data/protobuf/3.9.1/bincrafters/stable/package/053ea29eb0edc6b1695c893b738a971110c756fd/include/google/protobuf/message_lite.h:153: undefined reference to `google::protobuf::internal::fixed_address_empty_string[abi:cxx11]'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o: In function `google::protobuf::internal::WireFormatLite::ReadString(google::protobuf::io::CodedInputStream*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
/home/adam/.conan/data/protobuf/3.9.1/bincrafters/stable/package/053ea29eb0edc6b1695c893b738a971110c756fd/include/google/protobuf/wire_format_lite.h:880: undefined reference to `google::protobuf::internal::WireFormatLite::ReadBytes(google::protobuf::io::CodedInputStream*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o:(.data.rel.ro._ZTVN8tutorial11AddressBookE[_ZTVN8tutorial11AddressBookE]+0x20): undefined reference to `google::protobuf::Message::GetTypeName[abi:cxx11]() const'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o:(.data.rel.ro._ZTVN8tutorial11AddressBookE[_ZTVN8tutorial11AddressBookE]+0x58): undefined reference to `google::protobuf::Message::InitializationErrorString[abi:cxx11]() const'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o:(.data.rel.ro._ZTVN8tutorial6PersonE[_ZTVN8tutorial6PersonE]+0x20): undefined reference to `google::protobuf::Message::GetTypeName[abi:cxx11]() const'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o:(.data.rel.ro._ZTVN8tutorial6PersonE[_ZTVN8tutorial6PersonE]+0x58): undefined reference to `google::protobuf::Message::InitializationErrorString[abi:cxx11]() const'
CMakeFiles/NetworkMonitor.dir/addressbook.pb.cc.o:(.data.rel.ro._ZTVN8tutorial18Person_PhoneNumberE[_ZTVN8tutorial18Person_PhoneNumberE]+0x20): undefined reference to `google::protobuf::Message::GetTypeName[abi:cxx11]() const'
make[2]: *** [bin/N
在我為 conan 定義了一個配置文件並鏈接到使用相同版本的 C++ ABI 編譯的庫后,問題就消失了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.