From 27c4ca15359fc6902ab3c023fea7e0ef4a22951a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Korina=20=C5=A0imi=C4=8Devi=C4=87?= Date: Fri, 3 May 2024 11:27:11 +0200 Subject: [PATCH] Add a build with CMake example to README and Introduction page Summary: related to T13767 Reviewers: ivica Reviewed By: ivica Subscribers: miljen, iljazovic Differential Revision: https://repo.mireo.local/D29243 --- README.md | 25 ++++++++++++++++++++++--- doc/qbk/00_main.qbk | 2 +- doc/qbk/01_intro.qbk | 22 +++++++++++++++++++--- example/hello_world_over_tcp.cpp | 6 +++--- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4f987e7..dc63404 100644 --- a/README.md +++ b/README.md @@ -66,8 +66,7 @@ The following example illustrates a simple scenario of configuring a Client and int main() { boost::asio::io_context ioc; - using client_type = async_mqtt5::mqtt_client; - client_type c(ioc); + async_mqtt5::mqtt_client c(ioc); c.credentials("", "", "") .brokers("", 1883) @@ -85,7 +84,27 @@ int main() { ioc.run(); } ``` -To see more examples, visit [Examples](https://spacetime.mireo.com/async-mqtt5/async_mqtt5/examples.html). +To see more examples, visit [Examples](https://github.com/mireo/async-mqtt5/tree/master/example). + +Building with CMake +--------- +You can use the `CMakeLists.txt` provided in our repository to compile and run any of the [examples](https://github.com/mireo/async-mqtt5/tree/master/example) or your own source files. +The following commands demonstrate compiling and running the previous code using CMake. +The source file is located at [example/hello_world_over_tcp.cpp](https://github.com/mireo/async-mqtt5/blob/master/example/hello_world_over_tcp.cpp). + +```console + # navigate to the root folder of Async.MQTT5 + + # compile the example + cmake -S . -B {build-folder} -DBUILD_EXAMPLES=ON -DCMAKE_EXE_LINKER_FLAGS="-pthread" + cmake --build {build-folder} + + # run the example + ./{build-folder}/example/example +``` + +You can edit the [example/CMakeLists.txt](https://github.com/mireo/async-mqtt5/blob/master/example/CMakeLists.txt) file to compile the source file of your choice. +By default, it will compile [example/hello_world_over_tcp.cpp](https://github.com/mireo/async-mqtt5/blob/master/example/hello_world_over_tcp.cpp). When to use --------- diff --git a/doc/qbk/00_main.qbk b/doc/qbk/00_main.qbk index aa1edc5..68ddf9d 100644 --- a/doc/qbk/00_main.qbk +++ b/doc/qbk/00_main.qbk @@ -20,7 +20,7 @@ [template indexterm1[term1] ''''''[term1]''''''] [template indexterm2[term1 term2] ''''''[term1]''''''[term2]''''''] -[template ghreflink[path text] [@https://github.com/mireo/async-mqtt5/[path] [text]]] +[template ghreflink[path text] [@https://github.com/mireo/async-mqtt5/blob/master/[path] [text]]] [template reflink2[id text][link async_mqtt5.ref.[id] [^[text]]]] [template reflink[id][reflink2 [id] [id]]] [template refmem[class mem][reflink2 [class].[mem] [class]::[mem]]] diff --git a/doc/qbk/01_intro.qbk b/doc/qbk/01_intro.qbk index 121652d..e4e430c 100644 --- a/doc/qbk/01_intro.qbk +++ b/doc/qbk/01_intro.qbk @@ -45,7 +45,7 @@ In the event of a connection failure with one Broker, the Client switches to the [heading Example] The following example illustrates a simple scenario of configuring a Client and publishing a -"Hello World!" Application Message with `QoS` 0. +"Hello World!" Application Message with `QoS` 0. [!c++] #include @@ -59,8 +59,7 @@ The following example illustrates a simple scenario of configuring a Client and int main() { boost::asio::io_context ioc; - using client_type = async_mqtt5::mqtt_client; - client_type c(ioc); + async_mqtt5::mqtt_client c(ioc); c.credentials("", "", "") .brokers("", 1883) @@ -80,6 +79,23 @@ The following example illustrates a simple scenario of configuring a Client and To see more examples, visit [link async_mqtt5.examples Examples]. +[heading Building with CMake] +You can use the `CMakeLists.txt` provided in our repository to compile and run any of the [link async_mqtt5.examples examples] or your own source files. +The following commands demonstrate compiling and running the previous code using CMake. +The source file is located at [ghreflink example/hello_world_over_tcp.cpp example/hello_world_over_tcp.cpp]. + + # navigate to the root folder of Async.MQTT5 + + # compile the example + cmake -S . -B {build-folder} -DBUILD_EXAMPLES=ON -DCMAKE_EXE_LINKER_FLAGS="-pthread" + cmake --build {build-folder} + + # run the example + ./{build-folder}/example/example + +You can edit the [ghreflink example/CMakeLists.txt example/CMakeLists.txt] file to compile the source file of your choice. +By default, it will compile [ghreflink example/hello_world_over_tcp.cpp example/hello_world_over_tcp.cpp]. + [heading When to use] __Self__ might be suitable for you if any of the following statements is true: diff --git a/example/hello_world_over_tcp.cpp b/example/hello_world_over_tcp.cpp index 6e9ef25..be42b57 100644 --- a/example/hello_world_over_tcp.cpp +++ b/example/hello_world_over_tcp.cpp @@ -14,12 +14,12 @@ int main() { async_mqtt5::mqtt_client client(ioc); // 1883 is the default TCP MQTT port. - client.brokers("", 1883) + client.brokers("broker.hivemq.com", 1883) .async_run(boost::asio::detached); client.async_publish( - "", "Hello world!", - async_mqtt5::retain_e::no, async_mqtt5::publish_props {}, + "async-mqtt5/test", "Hello world!", + async_mqtt5::retain_e::yes, async_mqtt5::publish_props {}, [&client](async_mqtt5::error_code ec) { std::cout << ec.message() << std::endl; client.async_disconnect(boost::asio::detached);