How to build paho mqtt c++ on windows

风流意气都作罢 提交于 2020-03-03 05:25:13

问题


I have few json data which I need to upload on azure iot hub. I am writing code in c++ and need mqtt to publish all the data to iot hub. I am referring this github page: https://github.com/eclipse/paho.mqtt.cpp

But instruction on how to build it is a bit confusing and not seems to be working. Can anyone please explain how can I install mqtt in windows and can use it with visual studio c++. Please help. Thanks


回答1:


I struggled the last few days too and I finally got it running.

I succeeded using the CMake GUI and the Developer Console from Visual Studio 2019.

The instructions I (kinda) followed, were these: https://github.com/eclipse/paho.mqtt.cpp. Scroll down to the Windows part, it's actually very straight forward. However, instead of using the terminal for the cmake -BBuild ... commands I used the CMake GUI, where I configured the variables appropriately.

Installing paho.mqtt.c

So, as mentioned on the instructions, you first need to install paho.mqtt.c. For that, just clone the repo somewhere on your machine. Inside the paho.mqtt.c folder create a "build" folder.

Open the CMake GUI, then click on "Browse Source" and set the folder where the repo was cloned ../paho.mqtt.c/. For "Browse Build" select the build folder you just created ../paho.mqtt.c/build/

The click on "Configure" (I used the default Generator Visual Studio 16 2019). At this instance I didn't touch the configuration variables, so I just went ahead and clicked on "Generate".

CMake GUI for paho.mqtt.c

Then, open a developer console (press the windows key, type "developer" and open the Developer Command Prompt for VS 2019, or whatever version you use) and navigate to the paho.mqtt.c folder. There, according to the instructions on the github page, type the command:

cmake --build build --target install

This will install the paho.mqtt.c in C:\Program Files(x86)\Eclipse Paho C\. Note that this location can be changed by modifying the CMAKE_INSTALL_PREFIX variable on the CMake GUI to a custom location.

Installing paho.mqtt.cpp

The procedure is in nature the same: open the CMake GUI, select the folder that contains the source, then the build folder and then click on "Configure".

Now, since paho.mqtt.cpp relies on libraries from paho.mqtt.c, you must tell cmake where to find the corresponding paho.mqtt.c libraries.

In order to do this, configure the variables PAHO_MQTT_C_INCLUDE_DIRS and PAHO_MQTT_C_LIBRARIES.

  • PAHO_MQTT_C_INCLUDE_DIRS should point to the "include" folder inside the paho.mqtt.c installation, in my case: C:/Program Files (x86)/Eclipse Paho C/include
  • PAHO_MQTT_C_LIBRARIES I set up to point to the paho-mqtt3c.dll, in my case: C:/Program Files (x86)/Eclipse Paho C/bin/paho-mqtt3c.dll

The other options I left untouched.

Finally, go back to the Developer Command Prompt, navigate to the paho.mqtt.cpp folder and run cmake --build build --target install.

If everything goes well, paho.mqtt.cpp will be installed in C:/Program Files (x86)/paho-mqtt-cpp, according to the configuration variable CMAKE_INSTALL_PREFIX.

Now, you can reference both libraries in your c++ projects. Be aware that if you want to use the paho.mqtt.cpp library on your project you'll also have to include paho.mqtt.c.



来源:https://stackoverflow.com/questions/56455358/how-to-build-paho-mqtt-c-on-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!