Paho MQTT C++ 连接用户和密码
Paho MQTT C++ connection user and password
我正在实现一个客户端以通过 MQTT 发送数据,并且我正在使用 Paho MQTT c++ 库。
现在我需要添加对用户和密码身份验证的支持,当我尝试像这样设置它们时:
std::string user = "user";
std::string password = "password";
mqtt::connect_options connOpts;
connOpts.set_user_name(user);
connOpts.set_password(password);
然后我得到
undefined reference to mqtt::connect_options::set_user_name(std:string
const&)
但是在头文件中connection_options.h
/**
* Sets the user name to use for the connection.
* @param userName
*/
void set_user_name(const std::string& userName);
同样的事情发生在 set_password(密码);
我遇到的另一个问题是我无法保持连接,因为我不能在 class 中拥有全局对象 mqtt::async_client,我只能在发布功能。
提前致谢。
我 运行 解决了这个问题,并通过在 set_user_name 和 [=21= 中添加我自己的代码来修复它] connect_options.h 中的函数(未在 C++ 包装器的任何其他文件中初始化)。
void set_user_name(const std::string& userName){
const char * usr = userName.c_str();
opts_.username = usr;
}
void set_password(const std::string& password){
const char * pw = password.c_str();
opts_.password = pw;
}
我正在实现一个客户端以通过 MQTT 发送数据,并且我正在使用 Paho MQTT c++ 库。 现在我需要添加对用户和密码身份验证的支持,当我尝试像这样设置它们时:
std::string user = "user";
std::string password = "password";
mqtt::connect_options connOpts;
connOpts.set_user_name(user);
connOpts.set_password(password);
然后我得到
undefined reference to mqtt::connect_options::set_user_name(std:string const&)
但是在头文件中connection_options.h
/**
* Sets the user name to use for the connection.
* @param userName
*/
void set_user_name(const std::string& userName);
同样的事情发生在 set_password(密码);
我遇到的另一个问题是我无法保持连接,因为我不能在 class 中拥有全局对象 mqtt::async_client,我只能在发布功能。
提前致谢。
我 运行 解决了这个问题,并通过在 set_user_name 和 [=21= 中添加我自己的代码来修复它] connect_options.h 中的函数(未在 C++ 包装器的任何其他文件中初始化)。
void set_user_name(const std::string& userName){
const char * usr = userName.c_str();
opts_.username = usr;
}
void set_password(const std::string& password){
const char * pw = password.c_str();
opts_.password = pw;
}