0

I have been successfully able to generate buffer classes from .PROTO files, with this command:

$ protoc --cpp_out=%CD% addressbook.proto

This works and generates buffer classes addressbook.pb.h and addressbook.pb.cc files.

Now, when I write an application to use these buffer classes, I get linker errors. I tried a couple of different ways:

As in the shipped example’s Makefile and in online doc:

pkg-config --cflags protobuf

g++ myApplication.cc addressbook.pb.cc `pkg-config --cflags --libs protobuf`

<Fails with linker errors.>

Also, just going into the examples directory, and:

make cpp

fails with,

c++ add_person.cc addressbook.pb.cc -o add_person_cpp `pkg-config --cflags --libs protobuf`
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lprotobuf
collect2.exe: error: ld returned 1 exit status
make: *** [add_person_cpp] Error 1

I am sure that pkg-config is set correctly, because:

$ pkg-config --cflags protobuf

-Isomepath/install/include

And,

$ pkg-config --libs protobuf
-Lsomepath/install/lib/ -llibprotobuf

Then, as suggested here, I tried:

g++ -I somepath/install/include -L somepath/install/lib add_person.cc addressbook.pb.cc -lprotobuf

c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lprotobuf
collect2.exe: error: ld returned 1 exit status
make: *** [cpp] Error 1

and,

g++ -I somepath/install/include -L somepath/install/lib add_person.cc addressbook.pb.cc -llibprotobuf

Fails with linker errors.
…
C:\Users\usrname\AppData\Local\Temp\ccjC2zi3.o:add_person.cc:(.text+0x2bb): undefined reference to `google::protobuf::internal::VerifyVersion(int, int, char const*)'
C:\Users\usrname\AppData\Local\Temp\ccjC2zi3.o:add_person.cc:(.text+0x3b1): undefined reference to `google::protobuf::Message::ParseFromIstream(std::istream*)'
C:\Users\usrname\AppData\Local\Temp\ccjC2zi3.o:add_person.cc:(.text+0x475): undefined reference to `google::protobuf::Message::SerializeToOstream(std::ostream*) const'
C:\Users\usrname\AppData\Local\Temp\ccjC2zi3.o:add_person.cc:(.text+0x4cd): undefined reference to `google::protobuf::ShutdownProtobufLibrary()'
C:\Users\usrname\AppData\Local\Temp\ccjC2zi3.o:add_person.cc:(.text$_ZNK6google8protobuf5Arena9AllocHookEPKSt9type_infoj[__ZNK6google8protobuf5Arena9AllocHookEPKSt9type_infoj]+0x2e): undefined reference to `google::protobuf::Arena::OnArenaAllocation(std::type_info const*, unsigned int) const'
… [ a few hundred lines ]

I repeated the above examples after executing

set LD_LIBRARY_PATH=somepath\install\lib

I made sure the environment variable in Window's system settings point to the directory that contains libprotobuf.lib (why does the generated pkgconfig folder inside this lib folder has protobuf.pc) that includes these lines:

libdir=somepath/install/lib
Libs: -L${libdir} -lprotobuf 

Shouldn't this be

Libs: -L${libdir} -llibprotobuf 

Still, after setting the LD_LIBRARY_PATH same errors. I tried with g++ v4.9.2 and v6.3.0 and MSVC 2013, 2015, 2017 (using cl instead of g++).

Another thing I tried was to put the libprotobuf.lib inside MiGW/bin, thinking maybe g++ will find it, but it did not help.

Any ideas why g++/MSVC is unable to find these libraries or if I am doing something wrong?

  • All the tests passed when I ran nmake check after installation as suggested in README.
  • I uninstalled and reinstalled v3.4.0 and v3.5.0 and both have same behavior.

1 Answers1

1

Have you built protobuf libraries for Microsoft Windows with MSVC? You should not expect, that MinGW will work with a static library built with MSVC.

In order to link your example under Microsoft Windows you could write:

cl   -I somepath\install\include   somepath\install\lib\libprotobuf.lib   add_person.cc   addressbook.pb.cc

The compiler 'cl' sees file type '.lib' and will handle 'libprotobuf.lib' as a library.

VKlann
  • 26
  • 2