0

I am implementing a client to send data over MQTT and I am using Paho MQTT c++ libs. Now I need to add support to user and password authentication and when I try to set them like this:

std::string user = "user";
std::string password = "password";
mqtt::connect_options connOpts;
connOpts.set_user_name(user);
connOpts.set_password(password);

and I get

undefined reference to mqtt::connect_options::set_user_name(std:string const&)

but in the header file connection_options.h

/**
 * Sets the user name to use for the connection.
 * @param userName 
 */
void set_user_name(const std::string& userName);

same thing happens with set_password(password);

Another issue I have is that I haven't been able to keep my connection alive because I can not have the mqtt::async_client object global in the class, I can only create it inside the publish function.

Thanks in advance.

hardillb
  • 54,545
  • 11
  • 67
  • 105
Erick
  • 73
  • 7

2 Answers2

1

I ran into this issue, and fixed it by adding my own code in the set_user_name and set_password functions in connect_options.h (which aren't initialized in any other file in the C++ wrapper).

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;
}
ojathelonius
  • 685
  • 8
  • 26
  • Sorry I did not answer you before, I was busy with other stuff. I will try this and give you feedback if it worked. – Erick Nov 09 '16 at 19:02
0

Please try this:

auto connOpts = mqtt::connect_options_builder()     
    .clean_session()
    .user_name("User name here")                  // put username here
    .password(binary_ref_passwd)
    .will(mqtt::message(TOPIC, LWT_PAYLOAD, QOS))
    .finalize();
trung
  • 81
  • 1
  • 1
  • 5