ld.exe 找不到 Protobuf 3.4.0 的 -lprotobuf

ld.exe cannot find -lprotobuf for Protobuf 3.4.0

我已经成功地从 .PROTO 文件生成缓冲区 类,使用这个命令:

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

这有效并生成缓冲区 类 addressbook.pb.haddressbook.pb.cc 文件。

现在,当我编写使用这些缓冲区的应用程序时 类,我遇到链接器错误。我尝试了几种不同的方法:

shipped example’s Makefile 和在线文档中所述:

pkg-config --cflags protobuf

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

<Fails with linker errors.>

此外,刚进入 examples directory,并且:

make cpp

失败,

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

我确定 pkg-config 设置正确,因为:

$ pkg-config --cflags protobuf

-Isomepath/install/include

并且,

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

然后,按照建议here,我尝试了:

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

并且,

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 ]

我在执行后重复了上面的例子

set LD_LIBRARY_PATH=somepath\install\lib

我确定Window的系统设置中的环境变量指向包含libprotobuf.lib的目录(为什么这个lib文件夹中生成的pkgconfig文件夹有protobuf.pc)包括这些行:

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

这不应该是

Libs: -L${libdir} -llibprotobuf 

设置 LD_LIBRARY_PATH 后仍然出现同样的错误。我尝试使用 g++ v4.9.2 和 v6.3.0 以及 MSVC 2013、2015、2017(使用 cl 而不是 g++)。

我尝试的另一件事是将 libprotobuf.lib 放在 MiGW/bin 中,我想也许 g++ 会找到它,但没有帮助。

知道为什么 g++/MSVC 找不到这些库或者我做错了什么吗?

您是否使用 MSVC 为 Microsoft Windows 构建了 protobuf 库?您不应该期望 MinGW 可以使用使用 MSVC 构建的静态库。

为了 link 您在 Microsoft Windows 下的示例,您可以这样写:

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

编译器 'cl' 发现文件类型“.lib”并将 'libprotobuf.lib' 作为库处理。