Paho C++ Lib 集成:未解决的外部错误
Paho C++ Lib integration: Unresolved external errors
我正在尝试在我自己的 C++ 项目中使用 Paho C++ 库 (Visual Studio 2017)。
我已经按照解释 here 构建并安装了 Paho C 库和 Paho C++ 库。
到目前为止,一切都很好。之后,我在 Visual Studio 2017 年创建了一个新的(空白)C++ 项目,并在项目属性中应用了以下更改:
- 在选项卡 C/C++ / 常规中:
在 "Additional Include Directories" 中,我添加了两个包含路径(来自 paho c 和 paho c++ 安装路径):
- 在选项卡链接器/常规中:
在 "Additional Library Directories" 中,我添加了 lib 文件夹的路径(来自 paho c 和 paho c++ 安装路径):
- 在选项卡 Linker/Input 中:
在 "Additional Dependencies" 中,我添加了 paho c 和 paho c++ 库的库名称。
- 此外,我已经将 paho-mqtt3c.dll 复制到我的 C++ 项目的 Debug 文件夹中,并且包含了 topic_publish.cpp 示例文件(作为我的主要文件)。
问题:如果我尝试构建应用程序,我会收到 20 个链接错误。
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 ==========
编辑:
该库通过包含它来使用,如下所示:
#include "mqtt/async_client.h"
这是我使用的主要代码(它是来自 paho c++ 库的 "topic_publish.cpp" 示例):
// 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;
}
我认为问题出在 paho-mqttpp3-static.lib 文件中,但我不知道如何解决这个问题。有什么建议吗?
它终于按照 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'
我正在尝试在我自己的 C++ 项目中使用 Paho C++ 库 (Visual Studio 2017)。 我已经按照解释 here 构建并安装了 Paho C 库和 Paho C++ 库。 到目前为止,一切都很好。之后,我在 Visual Studio 2017 年创建了一个新的(空白)C++ 项目,并在项目属性中应用了以下更改:
- 在选项卡 C/C++ / 常规中: 在 "Additional Include Directories" 中,我添加了两个包含路径(来自 paho c 和 paho c++ 安装路径):
- 在选项卡链接器/常规中: 在 "Additional Library Directories" 中,我添加了 lib 文件夹的路径(来自 paho c 和 paho c++ 安装路径):
- 在选项卡 Linker/Input 中: 在 "Additional Dependencies" 中,我添加了 paho c 和 paho c++ 库的库名称。
- 此外,我已经将 paho-mqtt3c.dll 复制到我的 C++ 项目的 Debug 文件夹中,并且包含了 topic_publish.cpp 示例文件(作为我的主要文件)。
问题:如果我尝试构建应用程序,我会收到 20 个链接错误。
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 ==========
编辑: 该库通过包含它来使用,如下所示:
#include "mqtt/async_client.h"
这是我使用的主要代码(它是来自 paho c++ 库的 "topic_publish.cpp" 示例):
// 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;
}
我认为问题出在 paho-mqttpp3-static.lib 文件中,但我不知道如何解决这个问题。有什么建议吗?
它终于按照 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'