Rubber Duck Rubber Duck - 3 months ago 27
C++ Question

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?

Answer

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