Я пытаюсь использовать библиотеку Paho C ++ в своем собственном проекте C ++ (Visual Studio 2017). Я собрал и установил Paho C lib и Paho C ++ lib, как было объяснено здесь . Все идет нормально. После этого я создал новый (пустой) проект C ++ в Visual Studio 2017 и применил следующие изменения в свойствах проекта:
- На вкладке C / C ++ / General: в разделе «Дополнительные каталоги включения» я добавлены два пути включения (из обоих путей установки paho c и paho c ++):
В Tab Linker / General: в «Дополнительные каталоги библиотек» я добавил пути к папкам lib (из обоих путей установки paho c и paho c ++):
В Tab Linker / Input: в «Дополнительные зависимости» я добавил имена библиотек как paho c, так и библиотеки paho c ++.
Кроме того, я скопировал файл paho-mqtt3 c .dll в папку Debug моего C ++ - проекта и включил образец файла topic_publi sh. 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"
Это основной код, который я использую (это пример "topic_publi sh. cpp" из библиотеки paho c ++):
// 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-stati c .lib, но я не знаю, как решить эту проблему. Есть предложения?