Header only mode

By default, async_mqtt operates in header-only mode. Simply #include <async_mqtt/all.hpp> in your source file.

Separate compilation mode

Header-only mode is simple but can significantly increase your project’s build times. In this case, using separate compilation mode could help resolve the issue.

Set the compiler option to define the ASYNC_MQTT_SEPARATE_COMPILATION preprocessor macro for all translation units. This enables separate compilation mode. Then, prepare a source file async_mqtt.cpp as follows:

#include <async_mqtt/src.hpp>

This is for building the library code.

Other source files remain unchanged from the header-only mode. Simply #include <async_mqtt/all.hpp>. Defining ASYNC_MQTT_SEPARATE_COMPILATION automatically removes the library code present in async_mqtt.cpp from async_mqtt/all.hpp. Therefore, once you compile async_mqtt.cpp (which takes a long time due to instantiation), subsequent builds will have faster compilation times.

Examples

Separate compilation mode client

Separate compilation mode endpoint

Library build

This is an alternative method to build a separately compiled library.

  1. Add the ASYNC_MQTT_BUILD_LIB=ON definition to cmake.

  2. Build the target async_mqtt_separate.

For example, on Linux:

cmake -DASYNC_MQTT_BUILD_LIB=ON ..
make async_mqtt_separate

This generates lib/libasync_mqtt_separate.a.

You can link it to your application that is compiled with ASYNC_MQTT_SEPARATE_COMPILATION.

Ensure you provide the same option when building both async_mqtt_separate and your application. If the options differ, linker-related errors may occur.

You can build async_mqtt_separate in parallel. This is faster but requires more memory.

make async_mqtt_separate -j