File: cppgetstarted.md

package info (click to toggle)
ignition-msgs 8.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284 kB
  • sloc: cpp: 2,572; ansic: 238; ruby: 106; makefile: 18; sh: 17
file content (91 lines) | stat: -rw-r--r-- 1,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
\page cppgetstarted C++ Get Started

## Overview

This tutorial describes how to get started using Ignition Msgs with C++.

We will run through a simple example that populates a message and prints it.
Start by creating a bare-bones main file using the editor of your choice.

```{.cpp}
int main()
{
  return 0;
}
```

The easiest way to include Ignition Msgs is through the `ignition/msgs.hh`
header file. Alternatively, you can include only the header files you need.
For this example, we'll take the short and easy approach.

At this point your main file should look like

```{.cpp}
#include <ignition/msgs.hh>

int main()
{
  return 0;
}
```

Now let's create a message to store three points with arbitrary values.
We will use the `ignition::msgs::Vector3` class to store these points and the
function `DebugString()` to print the content on the terminal.


```{.cpp}
#include <iostream>
#include <ignition/msgs.hh>

int main()
{
  ignition::msgs::Vector3d point1;
  point1.set_x(1);
  point1.set_y(3);
  point1.set_z(5);
  ignition::msgs::Vector3d point2;
  point2.set_x(2);
  point2.set_y(4);
  point2.set_z(6);

  std::cout << "Point1:\n" << point1.DebugString() << std::endl;
  std::cout << "Point2:\n" << point2.DebugString() << std::endl;

  return 0;
}
```

To compile the code create a `CMakeLists.txt`:

```
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

# Find the Ignition msgs library
find_package(ignition-msgs8 QUIET REQUIRED)

add_executable(ignition-msgs-example main.cc)
target_link_libraries(ignition-msgs-example ${IGNITION-MSGS_LIBRARIES})
```

Let's start by compiling the examples:

```
mkdir build && cd build
cmake ..
make
```

Run the example, you should see something like:

```{.sh}
Point1:
x: 1
y: 3
z: 5

Point2:
x: 2
y: 4
z: 6
```