c++dllmqttpahopahocpp

Paho C++ Lib integration: Unresolved external errors


I am trying to use the Paho C++ library within my own C++ project (Visual Studio 2017). I have built and installed the Paho C lib and Paho C++ lib as it was explained here . So far so good. Afterwards, I created a new (blank) C++ project in Visual Studio 2017 and applied the following changes in the project properties:

  1. In Tab C/C++ / General: In "Additional Include Directories", I added two include paths (from both paho c and paho c++ installation path):

Additional Include Directories

  1. In Tab Linker / General: In "Additional Library Directories", I added the paths to the lib folders (from both paho c and paho c++ installation path):

Additional Library Directories

  1. In Tab Linker/Input: In "Additional Dependencies", I added the library names of both the paho c and paho c++ library.

enter image description here

  1. Furthermore, I have copied the paho-mqtt3c.dll into the Debug folder of my C++-project and I have included the topic_publish.cpp sample file (to be my main).

ISSUE: If I try to build the application, I am getting 20 linking errors.

1>------ Build started: Project: PahoCppLibTest, Configuration: Debug Win32 ------
1>Main.cpp
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_setDisconnected referenced in function "public: void __thiscall mqtt::async_client::set_disconnected_handler(class std::function<void __cdecl(class mqtt::properties const &,enum mqtt::ReasonCode)>)" (?set_disconnected_handler@async_client@mqtt@@QAEXV?$function@$$A6AXABVproperties@mqtt@@W4ReasonCode@2@@Z@std@@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_setCallbacks referenced in function "public: virtual void __thiscall mqtt::async_client::set_callback(class mqtt::callback &)" (?set_callback@async_client@mqtt@@UAEXAAVcallback@2@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_setConnectionLostCallback referenced in function "public: void __thiscall mqtt::async_client::set_connection_lost_handler(class std::function<void __cdecl(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)>)" (?set_connection_lost_handler@async_client@mqtt@@QAEXV?$function@$$A6AXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z@std@@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_setMessageArrivedCallback referenced in function "public: void __thiscall mqtt::async_client::set_message_callback(class std::function<void __cdecl(class std::shared_ptr<class mqtt::message const >)>)" (?set_message_callback@async_client@mqtt@@QAEXV?$function@$$A6AXV?$shared_ptr@$$CBVmessage@mqtt@@@std@@@Z@std@@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_setConnected referenced in function "public: virtual void __thiscall mqtt::async_client::set_callback(class mqtt::callback &)" (?set_callback@async_client@mqtt@@UAEXAAVcallback@2@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_reconnect referenced in function "public: virtual class std::shared_ptr<class mqtt::token> __thiscall mqtt::async_client::reconnect(void)" (?reconnect@async_client@mqtt@@UAE?AV?$shared_ptr@Vtoken@mqtt@@@std@@XZ)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_createWithOptions referenced in function "public: __thiscall mqtt::async_client::async_client(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0async_client@mqtt@@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0H0@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_connect referenced in function "public: virtual class std::shared_ptr<class mqtt::token> __thiscall mqtt::async_client::connect(class mqtt::connect_options)" (?connect@async_client@mqtt@@UAE?AV?$shared_ptr@Vtoken@mqtt@@@std@@Vconnect_options@2@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_disconnect referenced in function "public: virtual class std::shared_ptr<class mqtt::token> __thiscall mqtt::async_client::disconnect(class mqtt::disconnect_options)" (?disconnect@async_client@mqtt@@UAE?AV?$shared_ptr@Vtoken@mqtt@@@std@@Vdisconnect_options@2@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_isConnected referenced in function "public: virtual bool __thiscall mqtt::async_client::is_connected(void)const " (?is_connected@async_client@mqtt@@UBE_NXZ)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_subscribe referenced in function "public: virtual class std::shared_ptr<class mqtt::token> __thiscall mqtt::async_client::subscribe(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int,class mqtt::subscribe_options const &)" (?subscribe@async_client@mqtt@@UAE?AV?$shared_ptr@Vtoken@mqtt@@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@HABVsubscribe_options@2@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_subscribeMany referenced in function "public: virtual class std::shared_ptr<class mqtt::token> __thiscall mqtt::async_client::subscribe(class std::shared_ptr<class mqtt::string_collection const >,class std::vector<int,class std::allocator<int> > const &,class std::vector<class mqtt::subscribe_options,class std::allocator<class mqtt::subscribe_options> > const &)" (?subscribe@async_client@mqtt@@UAE?AV?$shared_ptr@Vtoken@mqtt@@@std@@V?$shared_ptr@$$CBVstring_collection@mqtt@@@4@ABV?$vector@HV?$allocator@H@std@@@4@ABV?$vector@Vsubscribe_options@mqtt@@V?$allocator@Vsubscribe_options@mqtt@@@std@@@4@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_unsubscribe referenced in function "public: virtual class std::shared_ptr<class mqtt::token> __thiscall mqtt::async_client::unsubscribe(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?unsubscribe@async_client@mqtt@@UAE?AV?$shared_ptr@Vtoken@mqtt@@@std@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@4@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_unsubscribeMany referenced in function "public: virtual class std::shared_ptr<class mqtt::token> __thiscall mqtt::async_client::unsubscribe(class std::shared_ptr<class mqtt::string_collection const >)" (?unsubscribe@async_client@mqtt@@UAE?AV?$shared_ptr@Vtoken@mqtt@@@std@@V?$shared_ptr@$$CBVstring_collection@mqtt@@@4@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_sendMessage referenced in function "public: virtual class std::shared_ptr<class mqtt::delivery_token> __thiscall mqtt::async_client::publish(class std::shared_ptr<class mqtt::message const >)" (?publish@async_client@mqtt@@UAE?AV?$shared_ptr@Vdelivery_token@mqtt@@@std@@V?$shared_ptr@$$CBVmessage@mqtt@@@4@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_freeMessage referenced in function "private: static int __cdecl mqtt::async_client::on_message_arrived(void *,char *,int,struct MQTTAsync_message *)" (?on_message_arrived@async_client@mqtt@@CAHPAXPADHPAUMQTTAsync_message@@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_free referenced in function "private: static int __cdecl mqtt::async_client::on_message_arrived(void *,char *,int,struct MQTTAsync_message *)" (?on_message_arrived@async_client@mqtt@@CAHPAXPADHPAUMQTTAsync_message@@@Z)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_destroy referenced in function "public: virtual __thiscall mqtt::async_client::~async_client(void)" (??1async_client@mqtt@@UAE@XZ)
1>paho-mqttpp3-static.lib(async_client.obj) : error LNK2019: unresolved external symbol _MQTTAsync_strerror referenced in function "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl mqtt::exception::error_str(int)" (?error_str@exception@mqtt@@SA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z)
1>C:\mqtt\projects\PahoCppLibTest\Debug\PahoCppLibTest.exe : fatal error LNK1120: 19 unresolved externals
1>Done building project "PahoCppLibTest.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

EDIT: The lib is used by including it like follows:

#include "mqtt/async_client.h"

This is the main code I am using (it's the "topic_publish.cpp" sample from the paho c++ library):

// topic_publish.cpp
//
// This is a Paho MQTT C++ client, sample application.
//
// It's an example of how to send messages as an MQTT publisher using the
// C++ asynchronous client interface using a 'topic' object to repeatedly
// send data to the same topic.
//
// The sample demonstrates:
//  - Connecting to an MQTT server/broker
//  - Publishing messages
//  - Use of the 'topic' class
//

/*******************************************************************************
 * Copyright (c) 2019 Frank Pagliughi <fpagliughi@mindspring.com>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *    Frank Pagliughi - initial implementation and documentation
 *******************************************************************************/

#include <iostream>
#include <cstdlib>
#include <string>
#include <thread>   // For sleep
#include <atomic>
#include <chrono>
#include <cstring>
#include "mqtt/async_client.h"

using namespace std;

const string DFLT_SERVER_ADDRESS{ "tcp://localhost:1883" };

const string TOPIC{ "test" };
const int QOS = 1;

const char* PAYLOADS[] = {
    "Hello World!",
    "Hi there!",
    "Is anyone listening?",
    "Someone is always listening.",
    nullptr
};

const auto TIMEOUT = std::chrono::seconds(10);

/////////////////////////////////////////////////////////////////////////////

int main(int argc, char* argv[])
{
    string address = (argc > 1) ? string(argv[1]) : DFLT_SERVER_ADDRESS;

    cout << "Initializing for server '" << address << "'..." << endl;
    mqtt::async_client cli(address, "");

    cout << "  ...OK" << endl;

    try {
        cout << "\nConnecting..." << endl;
        cli.connect()->wait();
        cout << "  ...OK" << endl;

        cout << "\nPublishing messages..." << endl;

        mqtt::topic top(cli, "test", QOS);
        mqtt::token_ptr tok;

        size_t i = 0;
        while (PAYLOADS[i]) {
            tok = top.publish(PAYLOADS[i++]);
        }
        tok->wait();    // Just wait for the last one to complete.
        cout << "OK" << endl;

        // Disconnect
        cout << "\nDisconnecting..." << endl;
        cli.disconnect()->wait();
        cout << "  ...OK" << endl;
    }
    catch (const mqtt::exception& exc) {
        cerr << exc.what() << endl;
        return 1;
    }

    return 0;
}

I suggest the problem lies within the paho-mqttpp3-static.lib file, but I do not know how to solve this. Any suggestions?


Solution

  • It finally works following the answer of fpagliughi (github):

    Yes, apologies. The build for this library is rather confusing. Hopefully the next update will make it easier.

    Part of the problem is that it requires the separate Paho C library and that one produces four different versions: two synchronous versions (secure and unsecure) and two asynchronous versions (also secure and unsecure).

    We only need one of the asynchronous versions: paho-mqtt3a.dll (no secure sockets) paho-mqtt3as.dll (includes SSL/TLS secure sockets)

    So, for your project, try removing 'paho-mqtt3c.dll' and replacing it with 'paho-mqtt3a.dll'