|
| 1 | +# Example Code: Modern C++ Distributed Logger Application |
| 2 | + |
| 3 | +## Example Structure |
| 4 | + |
| 5 | +The following files are part of the example: |
| 6 | + |
| 7 | +- `DistLoggerExample.cxx` |
| 8 | +- `distlogSupport.h` |
| 9 | + |
| 10 | +> **Note**: |
| 11 | +> |
| 12 | +> The `distlogSupport.h` header is necessary to use Modern C++ Distributed |
| 13 | +> Logger in RTI Connextdds 7.2.0. |
| 14 | +> |
| 15 | +> **Note**: |
| 16 | +> |
| 17 | +> The `CMakeLists.txt` file in this example uses the latest version |
| 18 | +> of `FindRTIConnextDDS.cmake` from the |
| 19 | +> [cmake-utils](https://github.com/rticommunity/rticonnextdds-cmake-utils) |
| 20 | +> repository to find the Modern C++ Distributed Logger libraries. |
| 21 | +> If you are using RTI Connext 7.2.0, you may want to use a newer version from |
| 22 | +> that repository as well. |
| 23 | +
|
| 24 | +## Building the Example |
| 25 | + |
| 26 | +In order to build this example, you need to define the variable `CONNEXTDDS_DIR` |
| 27 | +You can do so by exporting it manually, by sourcing the `rtisetenv` script for |
| 28 | +your architecture, or by passing it to the `cmake` command as arguments: |
| 29 | + |
| 30 | +```sh |
| 31 | +mkdir build |
| 32 | +cd build |
| 33 | +cmake -DCONNEXTDDS_DIR=<Connext DDS Directory> \ # If not exported |
| 34 | + -DBUILD_SHARED_LIBS=ON|OFF \ |
| 35 | + -DCMAKE_BUILD_TYPE=Debug|Release .. |
| 36 | +cmake --build . |
| 37 | +``` |
| 38 | + |
| 39 | +> **Note**: |
| 40 | +> |
| 41 | +> You do not need to define `CONNEXTDDS_ARCH` if you only have one architecture |
| 42 | +> target installed in your system. |
| 43 | +> |
| 44 | +> **Note**: |
| 45 | +> |
| 46 | +> When using a multi-configuration generator, make sure you specify |
| 47 | +> the `--config` parameter in your call to `cmake --build .`. In general, |
| 48 | +> it's a good practice to always provide it. |
| 49 | +
|
| 50 | +This will produce a binary directory (*build*) where the application |
| 51 | +can be found. |
| 52 | + |
| 53 | +> **Note:** |
| 54 | +> |
| 55 | +> When you need to cross-compile the example, the above |
| 56 | +> command will not work, the assigned compiler won't be the cross-compiler and |
| 57 | +> errors may happen when linking against the cross-compiled Connext binaries. To |
| 58 | +> fix this, you have to create a file with the architecture name and call CMake |
| 59 | +> with a specific flag called `-DCMAKE_TOOLCHAIN_FILE`. An example of the file to |
| 60 | +> create with the toolchain settings (e.g. for an ARM architectures): |
| 61 | +
|
| 62 | +```cmake |
| 63 | +set(CMAKE_SYSTEM_NAME Linux) |
| 64 | +set(toolchain_path "<path to>/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian") |
| 65 | +set(CMAKE_C_COMPILER "${toolchain_path}/bin/arm-linux-gnueabihf-gcc") |
| 66 | +set(CMAKE_CXX_COMPILER "${toolchain_path}/bin/arm-linux-gnueabihf-g++") |
| 67 | +``` |
| 68 | + |
| 69 | +Then you can call CMake like this: |
| 70 | + |
| 71 | +```sh |
| 72 | +cmake -DCONNEXTDDS_DIR=<connext dir> \ |
| 73 | + -DCMAKE_TOOLCHAIN_FILE=<toolchain file created above> \ |
| 74 | + -DCONNEXTDDS_ARCH=<connext architecture> .. |
| 75 | +``` |
| 76 | + |
| 77 | +## Running the Example |
| 78 | + |
| 79 | +### Example Application |
| 80 | + |
| 81 | +Run the application in a terminal as follows: |
| 82 | + |
| 83 | +On *Windows* systems run: |
| 84 | + |
| 85 | +```sh |
| 86 | +DistLoggerExample.exe [options] |
| 87 | +``` |
| 88 | + |
| 89 | +On *UNIX* systems run: |
| 90 | + |
| 91 | +```sh |
| 92 | +./DistLoggerExample [options] |
| 93 | +``` |
| 94 | + |
| 95 | +where the options are: |
| 96 | + |
| 97 | +- `-d, --domain`: Domain ID. |
| 98 | + |
| 99 | + **Default**: 0. |
| 100 | + |
| 101 | +- `-s, --sleep`: Number of seconds to sleep between iterations. |
| 102 | + |
| 103 | + **Default**: 1. |
| 104 | + |
| 105 | +- `-i, --iterations`: Number of logging iterations. |
| 106 | + |
| 107 | + **Default**: 50. |
| 108 | + |
| 109 | +- `-h, --help`: Displays application usage and exits. |
| 110 | + |
| 111 | +You should see the messages that are being logged on each iteration printed |
| 112 | +on the terminal. |
| 113 | + |
| 114 | +### Visualizing the log messages |
| 115 | + |
| 116 | +Once the example application is running, open RTI Spy or |
| 117 | +RTI Admin Console. |
| 118 | +You should be able to visualize the logging messages being sent |
| 119 | +by the application. |
| 120 | + |
| 121 | +To learn more about RTI Tools, refer to their section in the |
| 122 | +[Connext DDS documentation](https://community.rti.com/documentation). |
| 123 | + |
| 124 | +## Customizing the Build |
| 125 | + |
| 126 | +### Configuring Build Type and Generator |
| 127 | + |
| 128 | +By default, CMake will generate build files using the most common generator for |
| 129 | +your host platform (e.g., Makefiles on Unix-like systems and Visual Studio |
| 130 | +Solutions on Windows). You can use the following CMake variables to modify the |
| 131 | +default behavior: |
| 132 | + |
| 133 | +- `-DCMAKE_BUILD_TYPE` - specifies the build mode. Valid values are `Release` |
| 134 | + and `Debug`. See the [CMake documentation for more details |
| 135 | + (Optional)](https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html). |
| 136 | + |
| 137 | +- `-DBUILD_SHARED_LIBS` - specifies the link mode. Valid values are `ON` for |
| 138 | + dynamic linking and `OFF` for static linking. See [CMake documentation for |
| 139 | + more details |
| 140 | + (Optional)](https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html). |
| 141 | + |
| 142 | +- `-G` - CMake generator. The generator is the native build system used to |
| 143 | + build the source code. All the valid values are described in the CMake |
| 144 | + documentation for [CMake |
| 145 | + Generators](https://cmake.org/cmake/help/latest/manual/cmake-generators.7.html). |
| 146 | + |
| 147 | +For example, to build an example in Debug/Dynamic mode run CMake as follows: |
| 148 | + |
| 149 | +```sh |
| 150 | +cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_SHARED_LIBS=ON .. -G "Visual Studio 15 2017" -A x64 |
| 151 | +``` |
| 152 | + |
| 153 | +### Configuring Connext Installation Path and Architecture |
| 154 | + |
| 155 | +The CMake build infrastructure will try to guess the location of your Connext |
| 156 | +installation and the Connext architecture based on the default settings |
| 157 | +for your host platform. If you installed Connext in a custom location, you |
| 158 | +can use the `CONNEXTDDS_DIR` variable to indicate the path to your RTI Connext |
| 159 | +installation folder. For example: |
| 160 | + |
| 161 | +```sh |
| 162 | +cmake -DCONNEXTDDS_DIR=/home/rti/rti_connext_dds-x.y.z .. |
| 163 | +``` |
| 164 | + |
| 165 | +Also, if you installed libraries for multiple target architectures on your system |
| 166 | +(i.e., you installed more than one target `.rtipkg` file), you can use the |
| 167 | +`CONNEXTDDS_ARCH` variable to indicate the architecture of the specific libraries |
| 168 | +you want to link against. For example: |
| 169 | + |
| 170 | +```sh |
| 171 | +cmake -DCONNEXTDDS_ARCH=x64Linux3gcc5.4.0 .. |
| 172 | +``` |
0 commit comments