Program with Protocol Buffer can’t get linked

I have a cpp file in which I am trying to use the message that I created with protocol buffer. However I keep getting that famous “undefined reference” error as follows:

undefined reference to PointMsg::PointMsg()

undefined reference to PointMsg::~PointMsg()

undefined reference to PointMsg::~PointMsg()

Here is the proto file that I have, Point.proto :

message PointMsg {
   required float x = 1;
   required float y = 2;
   optional float z = 3;
   optional string name = 4 [default=""];

   required string sender = 5;
}

After executing proto command generating additional files which contain the necessary functions, I try to use them in my component, which is another file. I give the include as #include "proto/Point.pb.h" and I use it as follows:

PointMsg pointmsg;
detectObstacle(&pointmsg);

In the function what happens is:

void detectObstacle(PointMsg* pointmsg)
{   
    // initialize random seed
    srand (time(NULL));

    // generate two numbers
    int x = rand() % 800 + 1;
    int y = rand() % 800 + 1;

    // assign the values to fields
    pointmsg->set_x(x);
    pointmsg->set_y(y);

    cout<<pointmsg->x()<<endl;
    cout<<pointmsg->y()<<endl;

    string sender = "obstacle_detection";
    cout<<sender<<endl;

    pointmsg->set_sender(sender);
    cout<<pointmsg->sender()<<endl;

    return;
}

And here are the commands that use to build and link:

g++ -I $APR_INCLUDE -I $CMS_HOME/src/main -g -o obstacle_detection.o -c obstacle_detection.cpp

g++ -L $CMS_HOME/src/main/.libs/ -g -o obstacle_detection obstacle_detection.o -lactivemq-cpp -lssl -lprotobuf -pthread

And I get that error of undefined reference. I already spent a lot of time on this, couldn’t find any solution. People keep saying that I have to add the library at the end, well that’s not the solution for me.

Any help is appreciated.


Source: c++

Leave a Reply