basil
basil

Reputation: 720

cannot link against protobuf using c++ in linux

I am trying to just do a simple example for protobuf and I am having trouble linking. I haven't found anything worthwhile on here.

For starters, I have a very barebones .proto file

tempproj.proto

 package tempproj;
 message name
 {
     required string first = 1;
     required string last = 2;
 }

I am building it via:

$ protoc --cpp_out=. tempproj.proto

It is generating tempproj.pb.h and tempproj.pb.cc. I am assuming the generated code is correct.

I then have a file, main.cpp, which is barebones as well

 #include "tempproj.bh.h"

 int main(int argc, char** argv)
 {
      return 0;
 }

I then compile it via:

 $ g++ -std=gnu++14 -m64 -lprotobuf main.cpp tempproj.pb.cc

And the output is

 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o: in function `protobuf_tempproj_2eproto::protobuf_AssignDescriptors()':
 tempproj.pb.cc:(.text+0xf6): undefined reference to `google::protobuf::internal::AssignDescriptors(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::internal::MigrationSchema const*, google::protobuf::Message const* const*, unsigned int const*, google::protobuf::MessageFactory*, google::protobuf::Metadata*, google::protobuf::EnumDescriptor const**, google::protobuf::ServiceDescriptor const**)'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o: in function `protobuf_tempproj_2eproto::AddDescriptorsImpl()':
 tempproj.pb.cc:(.text+0x1a5): undefined reference to `google::protobuf::MessageFactory::InternalRegisterGeneratedFile(char const*, void (*)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&))'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o: in function `tempproj::name::SerializeWithCachedSizes(google::protobuf::io::CodedOutputStream*) const':
 tempproj.pb.cc:(.text+0xcb9): undefined reference to `google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::io::CodedOutputStream*)'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: tempproj.pb.cc:(.text+0xd24): undefined reference to `google::protobuf::internal::WireFormatLite::WriteStringMaybeAliased(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, google::protobuf::io::CodedOutputStream*)'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o: in function `google::protobuf::internal::GetEmptyStringAlreadyInited[abi:cxx11]()':
 tempproj.pb.cc:(.text._ZN6google8protobuf8internal27GetEmptyStringAlreadyInitedB5cxx11Ev[_ZN6google8protobuf8internal27GetEmptyStringAlreadyInitedB5cxx11Ev]+0x5): undefined reference to `google::protobuf::internal::fixed_address_empty_string[abi:cxx11]'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o: in function `google::protobuf::internal::WireFormatLite::ReadString(google::protobuf::io::CodedInputStream*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)':
 tempproj.pb.cc:(.text._ZN6google8protobuf8internal14WireFormatLite10ReadStringEPNS0_2io16CodedInputStreamEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN6google8protobuf8internal14WireFormatLite10ReadStringEPNS0_2io16CodedInputStreamEPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x1f): undefined reference to `google::protobuf::internal::WireFormatLite::ReadBytes(google::protobuf::io::CodedInputStream*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o: in function `google::protobuf::internal::WireFormatLite::WriteStringToArray(int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)':
 tempproj.pb.cc:(.text._ZN6google8protobuf8internal14WireFormatLite18WriteStringToArrayEiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPh[_ZN6google8protobuf8internal14WireFormatLite18WriteStringToArrayEiRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEPh]+0x39): undefined reference to `google::protobuf::io::CodedOutputStream::WriteStringWithSizeToArray(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned char*)'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o:(.rodata._ZTVN8tempproj4nameE[_ZTVN8tempproj4nameE]+0x20): undefined reference to `google::protobuf::Message::GetTypeName[abi:cxx11]() const'
 /opt/rh/gcc-toolset-11/root/usr/bin/ld: /tmp/ccj7rSmF.o:(.rodata._ZTVN8tempproj4nameE[_ZTVN8tempproj4nameE]+0x58): undefined reference to `google::protobuf::Message::InitializationErrorString[abi:cxx11]() const'
 collect2: error: ld returned 1 exit status

The paths are in normal places /usr/include for headers /usr/lib64 for libs.

Even when I explicitly set the lib path a la

 $ g++ -std=gnu++14 -m64 -L/usr/lib64 -lprotobuf main.cpp tempproj.pb.cc

I get the same results. I am rather stumped at this point.

FWIW I am using EL8, gcc-toolkit-11 (g++ 11.2.1)

edit: found the fix I believe: build with -D_GLIBCXX_USE_CXX11_ABI=0 (https://gcc.gnu.org/gcc-5/changes.html#libstdcxx)

Upvotes: 1

Views: 577

Answers (0)

Related Questions