Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Protobuf undefined reference to constructor/destructor

When attempting to compile code calling a small protobuf class (which itself successfully compiles), I get a linker error:

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

The line of code which causes this error:

ggf::Block blockMessage;

The protobuf:

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

And finally, my build line, config flags from pkg-config:

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

Alternatively, I can create a pointer to this Block class, which successfully compiles but then segfaults when setting the type,

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

Debugging reveals the following:

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

It's acting as if it's not initialized.. did I miss an initialization step?

like image 955
Rubber Duck Avatar asked Sep 01 '16 02:09

Rubber Duck


1 Answers

You should also add block.pb.cc to the compile file list:

g++ -o send sendercode.cpp block.pb.cc -std=c++11 -lprotobuf -pthread -g
like image 181
for_stack Avatar answered Sep 24 '22 05:09

for_stack