C++ API
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, andrclcpp_action::Server<A>/Client<A>. Every user-facing name is inrclcpp::,rclcpp_action::orrclcpp_lifecycle::; there is nonros::namespace. The extensions with no upstream counterpart (thePoll*types, the*Storagetypes,FixedString, …) are inrclcpp::too. - Errors are values. Every fallible call returns
rclcpp::Result, andRCLCPP_TRYreturns early on failure. Nothing throws. - Generated message types.
std_msgs::msg::Int32,example_interfaces::srv::AddTwoIntsand 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_STDadds thestd::string/std::function/std::chronooverloads and thestd::shared_ptr-returningrclcpp::Nodefactories. 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 includerclcpp::init_in/rclcpp::shutdown— session lifetimerclcpp::Node— node + create_publisher/subscription/service/client/action_*rclcpp::Publisher<M>/Subscription<M>. A subscription callback may also take upstream’sconst rclcpp::MessageInfo&as its second argument, freestanding too when the message isconst 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>::SharedPtrisrclcpp::ServiceHandle<S>, a two-word keep-alive with nothing to dereference;Client<S>::SharedPtrisrclcpp::ClientHandle<S>, the same two words with one verb,async_send_request. Neither has anoperator->.rclcpp::PollService<S>— L1 polling service server. The caller owns the server and drains it withtake_request/send_responsefrom 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_requestreturns arclcpp::Future<T>(on a hosted buildasync_send_request(shared_ptr)returns upstream’sFutureAndRequestId—.future,.request_id,share()),callblocks while driving the executor,call_pollingblocks without driving it, andwait_for_service/service_is_readyprobe 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 drivestake_*/accept_goal/complete_goalfrom a spin loop.rclcpp::Future<T>— async result handlerclcpp::Executor,Timer,GuardConditionrclcpp::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 thercl_interfacesservices read.rclcpp::Seq<T, N>is the array VALUE a freestanding node declares with (std::vector<T>underNROS_CPP_STD). There is no standalonerclcpp::ParameterServer: a second store would be invisible toros2 param get. See How nano-ros differs from ROS 2 for what is intentionally smaller thanrclcpp, and Parameters and/parameter_events.
Two-layer API. The
rclcpp::ActionServer<A>/ActionClient<A>templates are L2 callback handles, dispatched by the executor. Therclcpp::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>andPollingActionClient<A>expose a nestedWakeStatePOD and typedset_{goal,cancel,get_result,goal_response, cancel_response,result,feedback}_wake_callback(state, cb, ctx)methods. Pair annros_cpp_wake_state_twith 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
- rclcpp compatibility: what a ported rclcpp node gets, by the upstream spelling it uses, and the measured ROS 2 examples.
- Add a second node: a C++ node added to a workspace, built and run.
- A Zephyr app you build with west: a C++ node in a Zephyr application, under the nano-ros Zephyr module.
Design background
- RFC-0018: C++ API design — freestanding C++14, no exceptions, the
Resultchannel - RFC-0089: ROS 2 API adoption, and the compile-or-conform rule — upstream’s namespaces and names, the
_inrule, the hosted surface - RFC-0021: blocking API rules — why blocking calls drive the executor and refuse re-entry