C++ Protobuf 未定义引用 constructor/destructor

C++ Protobuf undefined reference to constructor/destructor

尝试编译调用小型 protobuf class 的代码(它本身已成功编译)时,出现链接器错误:

undefined reference to `ggf::Block::Leveling()'
undefined reference to `ggf::Block::~Leveling()'

导致此错误的代码行:

ggf::Block blockMessage;

protobuf:

package ggf;
message Leveling {
        required int32 type         = 1;
        optional uint64 blockNumber = 2;
        optional bytes blockData    = 3;
}

最后,我的构建行,来自 pkg-config 的配置标志:

g++ -o send sendercode.cpp -std=c++11 -lprotobuf -pthread -lpthread -g

或者,我可以创建一个指向此块的指针 class,它编译成功但在设置类型时出现段错误,

ggf::Block *blockRequest;
blockRequest->set_type(10);   //SIGSEGV

调试显示以下内容:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400d66 in ggf::Block::set_has_type (this=0x0) at block.pb.h:172

它就像没有初始化一样..我是不是错过了初始化步骤?

看看你的include,如果只包含"block.pb.h"文件就会出现这个问题。

您还应该将block.pb.cc添加到编译文件列表中:

g++ -o send sendercode.cpp block.pb.cc -std=c++11 -lprotobuf -pthread -g