Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

C++ API

Reference · Level 2: Building real applications

The nano-ros C++ API is a freestanding C++14 interface for ROS 2 nodes on embedded and host targets. It needs no standard library, no exceptions and no RTTI, so the same code builds for Zephyr, FreeRTOS, NuttX, ThreadX, bare metal and Linux.

  • Upstream’s names, in upstream’s namespaces. rclcpp::Node, Publisher<M>, Subscription<M>, Service<S>, Client<S>, Timer, GuardCondition, Executor, and rclcpp_action::Server<A> / Client<A>. Every user-facing name is in rclcpp::, rclcpp_action:: or rclcpp_lifecycle::; there is no nros:: namespace. The extensions with no upstream counterpart (the Poll* types, the *Storage types, FixedString, …) are in rclcpp:: too.
  • Errors are values. Every fallible call returns rclcpp::Result, and RCLCPP_TRY returns early on failure. Nothing throws.
  • Generated message types. std_msgs::msg::Int32, example_interfaces::srv::AddTwoInts and so on, generated by the build from the packages your node depends on (see Message Generation).
  • An opt-in std surface for ported code. Defining NROS_CPP_STD adds the std::string / std::function / std::chrono overloads and the std::shared_ptr-returning rclcpp::Node factories. The build requests it; it is never detected from the toolchain. See Two surfaces.

The headers live in the SDK’s C++ include directory at upstream’s paths: rclcpp/, rclcpp_action/ and rclcpp_lifecycle/. rclcpp/detail/ is internal.

To build and run a first C++ node, start with the Tutorial, whose every step has a C++ tab; to grow it into a workspace, see Organize a larger workspace. To bring an existing ROS 2 node across, see Porting a ROS 2 C++ node. This page is the API itself.

API reference

C++ API Doxygen — start here.

The Doxygen site is generated from the current main branch. The umbrella header rclcpp/rclcpp.hpp pulls in every public surface a user application needs.

Where to start

  • rclcpp/rclcpp.hpp — convenience umbrella include
  • rclcpp::init_in / rclcpp::shutdown — session lifetime
  • rclcpp::Node — node + create_publisher/subscription/service/client/action_*
  • rclcpp::Publisher<M> / Subscription<M>. A subscription callback may also take upstream’s const rclcpp::MessageInfo& as its second argument, freestanding too when the message is const M& (see rclcpp compatibility)
  • rclcpp::Service<S> / Client<S> — the DISPATCH service server and client. A handler is registered into the executor arena, which owns the entity. Service<S>::SharedPtr is rclcpp::ServiceHandle<S>, a two-word keep-alive with nothing to dereference; Client<S>::SharedPtr is rclcpp::ClientHandle<S>, the same two words with one verb, async_send_request. Neither has an operator->.
  • rclcpp::PollService<S> — L1 polling service server. The caller owns the server and drains it with take_request / send_response from its own spin loop. node.create_service<S>(name) with no handler hands one back.
  • rclcpp::PollClient<S> — L1 future-style service client, the client half of the same split. The caller owns the client and drains the reply itself: send_request returns a rclcpp::Future<T> (on a hosted build async_send_request(shared_ptr) returns upstream’s FutureAndRequestId — .future, .request_id, share()), call blocks while driving the executor, call_polling blocks without driving it, and wait_for_service / service_is_ready probe the server. node.create_client<S>(name) with no handler hands one back.
  • rclcpp_action::Server<A> / Client<A> — L2 callback model (executor-arena registered). rclcpp::ActionServer<A> / rclcpp::ActionClient<A> are the same two types. Hosted, upstream’s goal handles too (see rclcpp compatibility).
  • rclcpp::PollingActionServer<A> / PollingActionClient<A> — L1 polling model. Caller drives take_* / accept_goal / complete_goal from a spin loop.
  • rclcpp::Future<T> — async result handle
  • rclcpp::Executor, Timer, GuardCondition
  • rclcpp::trace_marker(id, arg) — an application event (ids 1-15) through the installed trace sink, ordered with the executor’s callback events; see Trace callbacks
  • Parameters are methods ON the node — declare_parameter<T> / get_parameter<T> / set_parameter<T> / has_parameter, upstream’s names, forwarding to the one store the rcl_interfaces services read. rclcpp::Seq<T, N> is the array VALUE a freestanding node declares with (std::vector<T> under NROS_CPP_STD). There is no standalone rclcpp::ParameterServer: a second store would be invisible to ros2 param get. See How nano-ros differs from ROS 2 for what is intentionally smaller than rclcpp, and Parameters and /parameter_events.

Two-layer API. The rclcpp::ActionServer<A> / ActionClient<A> templates are L2 callback handles, dispatched by the executor. The rclcpp::PollingActionServer<A> / PollingActionClient<A> are L1 polling templates with the same typed serialization glue but caller-driven control flow. Subscriptions, services and service clients have both shapes too (Subscription / PollSubscription, and so on). See Two ways to receive for the per-layer rationale.

Event-driven wake callbacks. PollingActionServer<A> and PollingActionClient<A> expose a nested WakeState POD and typed set_{goal,cancel,get_result,goal_response, cancel_response,result,feedback}_wake_callback(state, cb, ctx) methods. Pair an nros_cpp_wake_state_t with each entity / channel to wake on rx instead of polling.

Errors

Every fallible call returns an rclcpp::Result; RCLCPP_TRY returns early when one fails, and ret.raw() is the rclcpp::ErrorCode. The codes, with their meanings, are on the rclcpp/result.hpp page of the Doxygen site. A Full from a create_* usually means a build-time pool is too small; Static Pool Inventory names the knob that sizes it.

How to read the Doxygen site

The site lists every header under its upstream path, so a page you know from rclcpp’s documentation is at the same name: rclcpp/node.hpp, rclcpp_action/client.hpp, rclcpp_lifecycle/lifecycle_node.hpp. Each class page documents the freestanding surface first; members that exist only on a hosted build say NROS_CPP_STD. The pages under rclcpp/detail/ are internal and may change without notice.

See also

Design background