YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Compiling a YARP hello world program

The easiest way to write your first program using YARP is with CMake.

Make a directory called hello, and within it put a CMakeLists.txt file like this:

# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
# SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
# SPDX-License-Identifier: BSD-3-Clause
# YARP needs CMake 3.0 or greater
cmake_minimum_required(VERSION 3.19)
# find YARP
find_package(YARP COMPONENTS os REQUIRED)
# set up our program
add_executable(hello)
# declare our source files
target_sources(hello PRIVATE hello.cpp)
# link with YARP libraries
target_link_libraries(hello PRIVATE YARP::YARP_os
YARP::YARP_init)

And here's a simple test program, call it hello.cpp

/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <yarp/os/all.h>
#include <stdio.h>
using namespace yarp::os;
int main(int argc, char *argv[]) {
// Set up YARP
// Make two ports called /hello/in and /hello/out
// We'll send "Bottles" (a simple nested list container) between these ports
bool ok = inPort.open("/hello/in");
ok = ok && outPort.open("/hello/out");
if (!ok) {
fprintf(stderr, "Failed to create ports.\n");
fprintf(stderr, "Maybe you need to start a nameserver (run 'yarpserver')\n");
return 1;
}
// Make a connection between the output port and the input port
for (int i=0; i<10; i++) {
// prepare a message to send
out.clear();
out.addString("Hello");
out.addInt32(i);
printf("Sending %s\n", out.toString().c_str());
// send the message
outPort.write(true);
// read the message
Bottle *in = inPort.read();
if (in==NULL) {
fprintf(stderr, "Failed to read message\n");
return 1;
}
printf("Received %s\n", in->toString().c_str());
}
return 0;
}
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
void clear()
Empties the bottle of any objects it contains.
Definition Bottle.cpp:121
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
Definition Bottle.cpp:140
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition Bottle.cpp:211
A mini-server for performing network communication in the background.
std::string getName() const override
Get name of port.
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
T * read(bool shouldWait=true) override
Read an available object from the port.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
T & prepare()
Access the object which will be transmitted by the next call to yarp::os::BufferedPort::write.
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition Network.h:706
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.
Definition dirs.h:16

Now make a build directory inside the hello folder, then from the build directory run CMake (see Using CMake), and compile!

This example is available in the example/cmake/hello directory of the YARP source code.