无法获取 C++ 中的 protobuf 示例代码 link 步骤
Can't get the protobuf example code link step in C++
我正在尝试编译 github source for the protobuf 中提供的示例代码(添加地址,然后以序列化的方式将其发送到另一个反序列化并显示它的程序)。
protobuf源码编译安装成功。 (版本 3.13.0)。
挑战始于我尝试编译 C++ 示例文件 (add_person.cpp)。
我在 VS 2019 中创建了一个空白的 C++ 项目,然后粘贴了源代码行,并设置了额外的包含路径以包含 protobuf 安装 directory/include,以及 protobuf 安装的额外 lib 路径 directory/lib. addressbook.proto(来自示例)已编译,.cc 文件作为附加文件包含在该玩具项目中。头文件(.pb.h)在代码中已经被引用,所有的include文件等都在编译时没有报错
但是,在链接阶段,它抛出了 79 个错误,其中大部分显然是缺少某些库。它还显示了 163 个警告。此处重现了一些初始错误。
'''
错误 LNK2001 未解析的外部符号“private: unsigned char * __cdecl google::protobuf::io::EpsCopyOutputStream::WriteStringMaybeAliasedOutline(unsigned int,class std::basic_string const &,unsigned char *)" (?WriteStringMaybeAliasedOutline@EpsCopyOutputStream@io@protobuf@google@@AEAAPEAEIAEBV?$basic_string@DU?$char_traits@D @std@@V?$allocator@D@2@@std@@PEAE@Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
错误 LNK2001 未解析的外部符号“private: unsigned char * __cdecl google::protobuf::io::EpsCopyOutputStream::EnsureSpaceFallback(unsigned char *)” (?EnsureSpaceFallback@EpsCopyOutputStream@io@protobuf@google@@AEAAPEAEPEAE @Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
错误 LNK2001 未解析的外部符号“void __cdecl google::protobuf::internal::InitSCCImpl(struct google::protobuf::internal::SCCInfoBase *)” (?InitSCCImpl@internal@protobuf@google@@YAXPEAUSCCInfoBase@123 @@Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
错误 LNK2001 未解析的外部符号“void __cdecl google::protobuf::internal::VerifyVersion(int,int,char const *)” (?VerifyVersion@internal@protobuf@google@@YAXHHPEBD@Z ) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
Error LNK2001 未解析的外部符号“void __cdecl google::protobuf::internal::AssignDescriptors(struct google::protobuf::internal::DescriptorTable const *,bool)” (?AssignDescriptors@internal@protobuf@google@@ YAXPEBUDescriptorTable@123@_N@Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
错误 LNK2001 未解析的外部符号“public: void __cdecl google::protobuf::internal::LogFinisher::operator=(class google::protobuf::internal::LogMessage &)” (??4LogFinisher@internal@ protobuf@google@@QEAAXAEAVLogMessage@123@@Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
'''
很明显,某些库似乎丢失或未正确链接。但是,为其他库指定的目录中似乎存在 3 个库。我显然是 protobuf 的新手。欢迎任何建议。
谢谢
缺少库,因为您只指定了库目录。您是否真的链接了所需的库?因为指定 libdirs(-L 用于 gcc)只是为了方便,所以您可以使用 -lmylibname
而不是指定完整路径(查看 this 文章的底部以查看区别)。
因此,就 g++ 指令而言,假设 main.o
需要与 libyour.a
链接并且 lib 在 /lib 目录中,您已完成以下操作:
g++ main.o -L/lib
您可以清楚地看到库没有被链接。您必须做的是:
g++ main.o -L/lib -lyour
(lib 正在被链接器追加)。
正如您自己在评论中注意到的那样,考虑到您使用的是 VS,有一个您应该配置的 Additional Dependencies 开关。
我正在尝试编译 github source for the protobuf 中提供的示例代码(添加地址,然后以序列化的方式将其发送到另一个反序列化并显示它的程序)。
protobuf源码编译安装成功。 (版本 3.13.0)。
挑战始于我尝试编译 C++ 示例文件 (add_person.cpp)。
我在 VS 2019 中创建了一个空白的 C++ 项目,然后粘贴了源代码行,并设置了额外的包含路径以包含 protobuf 安装 directory/include,以及 protobuf 安装的额外 lib 路径 directory/lib. addressbook.proto(来自示例)已编译,.cc 文件作为附加文件包含在该玩具项目中。头文件(.pb.h)在代码中已经被引用,所有的include文件等都在编译时没有报错
但是,在链接阶段,它抛出了 79 个错误,其中大部分显然是缺少某些库。它还显示了 163 个警告。此处重现了一些初始错误。
'''
错误 LNK2001 未解析的外部符号“private: unsigned char * __cdecl google::protobuf::io::EpsCopyOutputStream::WriteStringMaybeAliasedOutline(unsigned int,class std::basic_string
错误 LNK2001 未解析的外部符号“private: unsigned char * __cdecl google::protobuf::io::EpsCopyOutputStream::EnsureSpaceFallback(unsigned char *)” (?EnsureSpaceFallback@EpsCopyOutputStream@io@protobuf@google@@AEAAPEAEPEAE @Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
错误 LNK2001 未解析的外部符号“void __cdecl google::protobuf::internal::InitSCCImpl(struct google::protobuf::internal::SCCInfoBase *)” (?InitSCCImpl@internal@protobuf@google@@YAXPEAUSCCInfoBase@123 @@Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
错误 LNK2001 未解析的外部符号“void __cdecl google::protobuf::internal::VerifyVersion(int,int,char const *)” (?VerifyVersion@internal@protobuf@google@@YAXHHPEBD@Z ) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
Error LNK2001 未解析的外部符号“void __cdecl google::protobuf::internal::AssignDescriptors(struct google::protobuf::internal::DescriptorTable const *,bool)” (?AssignDescriptors@internal@protobuf@google@@ YAXPEBUDescriptorTable@123@_N@Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
错误 LNK2001 未解析的外部符号“public: void __cdecl google::protobuf::internal::LogFinisher::operator=(class google::protobuf::internal::LogMessage &)” (??4LogFinisher@internal@ protobuf@google@@QEAAXAEAVLogMessage@123@@Z) example_proto C:\Users\quasa\source\repos\example_proto\addressbook.pb.obj 1
'''
很明显,某些库似乎丢失或未正确链接。但是,为其他库指定的目录中似乎存在 3 个库。我显然是 protobuf 的新手。欢迎任何建议。
谢谢
缺少库,因为您只指定了库目录。您是否真的链接了所需的库?因为指定 libdirs(-L 用于 gcc)只是为了方便,所以您可以使用 -lmylibname
而不是指定完整路径(查看 this 文章的底部以查看区别)。
因此,就 g++ 指令而言,假设 main.o
需要与 libyour.a
链接并且 lib 在 /lib 目录中,您已完成以下操作:
g++ main.o -L/lib
您可以清楚地看到库没有被链接。您必须做的是:
g++ main.o -L/lib -lyour
(lib 正在被链接器追加)。
正如您自己在评论中注意到的那样,考虑到您使用的是 VS,有一个您应该配置的 Additional Dependencies 开关。