即使我已经包含了标题和库(这是自编译的),链接错误

Linking errors even though I've included headers and library (which is self-compiled)

本文关键字:编译 错误 链接 包含 标题      更新时间:2023-10-16

我已经尝试了几个小时来让 protobuf 工作,但感觉我已经尝试了所有要尝试的事情,但我仍然无法在没有这些错误的情况下让它链接:

LNK2019 unresolved external symbol "public: __thiscall tutorial::Person_PhoneNumber::Person_PhoneNumber(void)" (??0Person_PhoneNumber@tutorial@@QAE@XZ) referenced in function "public: static class tutorial::Person_PhoneNumber * __cdecl google::protobuf::Arena::Create<class tutorial::Person_PhoneNumber>(class google::protobuf::Arena *)" (??$Create@VPerson_PhoneNumber@tutorial@@@Arena@protobuf@google@@SAPAVPerson_PhoneNumber@tutorial@@PAV012@@Z) 
LNK2019 unresolved external symbol "public: __thiscall tutorial::Person::Person(void)" (??0Person@tutorial@@QAE@XZ) referenced in function "public: static class tutorial::Person * __cdecl google::protobuf::Arena::Create<class tutorial::Person>(class google::protobuf::Arena *)" (??$Create@VPerson@tutorial@@@Arena@protobuf@google@@SAPAVPerson@tutorial@@PAV012@@Z)
LNK2019 unresolved external symbol "public: __thiscall tutorial::AddressBook::AddressBook(void)" (??0AddressBook@tutorial@@QAE@XZ) referenced in function _main
LNK2019 unresolved external symbol "public: virtual __thiscall tutorial::AddressBook::~AddressBook(void)" (??1AddressBook@tutorial@@UAE@XZ) referenced in function _main    

我正在使用MS Visual Studio 2015。包含库和头文件(如果我重命名 .lib 文件,它会抱怨找不到它)。

我已经尝试了 2.6.1 和 3.0.0b3(protoc 和 libprotobuf,都是自编译的和不是)。我自己编译了protoc并用它编译了示例.proto文件,但它仍然无法编译protobuf附带的示例。我已经在调试和发布模式下编译了我自己的项目和 protobuf,并且我或多或少地将 protobuf 自己的测试项目中的每个设置复制到我自己的测试项目中,但它无法编译。

感觉就像我错过了一些超级简单的东西,但是我已经用谷歌搜索了几个小时,我找不到我做错了什么。

即使是像下面这样的超级简单的文件也会生成相同的错误。

#include "example.pb.h"
int main()
{
    GOOGLE_PROTOBUF_VERIFY_VERSION;
    Example ex;
}

还有什么可做的/尝试的?

当你在 example.proto 上运行 protoc 时,它会生成两个文件:example.pb.hexample.pb.cc 。您需要将example.pb.cc编译到程序中。