带有 Protocol Buffer 的程序无法链接
Program with Protocol Buffer can't get linked
我有一个 cpp 文件,我试图在其中使用我通过协议缓冲区创建的消息。但是,我不断收到如下著名的 "undefined reference" 错误:
undefined reference to PointMsg::PointMsg()
undefined reference to PointMsg::~PointMsg()
undefined reference to PointMsg::~PointMsg()
这是我的原型文件,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;
}
执行 proto 命令生成包含必要功能的附加文件后,我尝试在我的组件中使用它们,这是另一个文件。我将包含作为 #include "proto/Point.pb.h"
并按如下方式使用它:
PointMsg pointmsg;
detectObstacle(&pointmsg);
函数中发生的事情是:
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;
}
下面是用于构建和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
我得到了未定义引用的错误。我已经在这上面花了很多时间,找不到任何解决方案。人们一直说我必须在最后添加库,但这不是我的解决方案。
感谢任何帮助。
您需要 运行 proto 编译器生成特定于您的 protobuf 模式的 C++ 代码(您似乎已经完成),然后编译并 link 代码也是!某处应该有类似 Point.pb.o
文件的东西。
没有一个固定的库可以为所有未来的消息类型提供代码。 public 库仅包含与类型无关的核心操作(例如字段描述符、序列化、重复字段)的通用代码。但是,为您的消息类型提供实际 C++ 接口的叶代码成为 您的 代码的一部分,您必须自己 link。
我有一个 cpp 文件,我试图在其中使用我通过协议缓冲区创建的消息。但是,我不断收到如下著名的 "undefined reference" 错误:
undefined reference to PointMsg::PointMsg()
undefined reference to PointMsg::~PointMsg()
undefined reference to PointMsg::~PointMsg()
这是我的原型文件,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;
}
执行 proto 命令生成包含必要功能的附加文件后,我尝试在我的组件中使用它们,这是另一个文件。我将包含作为 #include "proto/Point.pb.h"
并按如下方式使用它:
PointMsg pointmsg;
detectObstacle(&pointmsg);
函数中发生的事情是:
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;
}
下面是用于构建和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
我得到了未定义引用的错误。我已经在这上面花了很多时间,找不到任何解决方案。人们一直说我必须在最后添加库,但这不是我的解决方案。
感谢任何帮助。
您需要 运行 proto 编译器生成特定于您的 protobuf 模式的 C++ 代码(您似乎已经完成),然后编译并 link 代码也是!某处应该有类似 Point.pb.o
文件的东西。
没有一个固定的库可以为所有未来的消息类型提供代码。 public 库仅包含与类型无关的核心操作(例如字段描述符、序列化、重复字段)的通用代码。但是,为您的消息类型提供实际 C++ 接口的叶代码成为 您的 代码的一部分,您必须自己 link。