5

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

S Andrew
  • 5,592
  • 27
  • 115
  • 237

1 Answers1

7

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.

aapa320
  • 392
  • 4
  • 10
  • how can I integrate the Paho C++ lib into my Visual Studio C++ Blank Project. Which project properties do I have to set? Besides, thank you so much for the detailed answer!! It really helped me a lot. – user7335295 Feb 24 '20 at 13:39
  • 2
    When you build the project, inside the build directory, there will be a visual studio solution that you can just open. The solution will contain a few different projects, among them your own. – aapa320 Feb 25 '20 at 15:44