无法从谷歌协议缓冲区编译示例

时间:2012-05-01 20:40:14

标签: c++ linux compiler-errors g++ 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):未定义引用   `谷歌:: protobuf的::内部:: kEmptyString'

我认为,这是一个链接问题,但如何解决呢?

  

echo $ LD_LIBRARY_PATH   的/ home /用户名/本地/ lib中

在main.cpp中:

#include "person.pb.h"
...

感谢。

3 个答案:

答案 0 :(得分:20)

把图书馆放在最后:

  

g ++ -I / home / username / local / include -L / home / username / local / lib main.cpp person.pb.cc -lprotobuf -pthread

来自GCC Link Options

-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可以为预处理器和链接器设置标志。

答案 1 :(得分:2)

库链接标志位于编译器参数的末尾:

  

g ++ -I / home / username / local / include -L / home / username / local / lib   main.cpp person.pb.cc -lprotobuf -lpthread

答案 2 :(得分:0)

还有一个相当普遍的问题,不仅适用于链接protobuf,还适用于其他库。由于这是我的问题,因此我将解决方案放在这里,也许有人会觉得有用。

确保您要尝试链接到在同一GCC ABI上编译的库。 GCC ABI在GCC 4.7.0中已更改。因此,您无法将使用GCC <4.7.0编译的库(如果是从我的情况下从软件包存储库中获取软件包,则可能是protobuf)与使用GCC> 4.7.0编译的库链接。

可以通过让植物连接抱怨std :: basic_string和std :: list来识别此问题,因为这是最主要的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

在我为柯南定义了配置文件并链接到使用同一GCC ABI编译的库后,问题就消失了。