Browse by type

Welcome to our Modern C++ Course, specifically tailored for Engineers focusing on Robotics.
Mutex and lock_guardstd::scoped_lockstd::shared_mutexjoin vs detachcondition_variablestd::async & std::futurestd::atomicvariant, any, tuple)
Phase: Foundations
C++ is a high-performance, compiled language created by Bjarne Stroustrup. In robotics, it is the industry standard for real-time systems, providing "zero-cost abstractions" that allow for high-level code with maximum hardware efficiency.
[!NOTE] Design Philosophy: This module is designed to demystify the "black box" of how code reaches hardware. Robotics involves diverse hardware (ARM/x86); if you don't understand the Linker vs. Compiler, you will be paralyzed by the first library error you hit in ROS.
[!NOTE] Modern C++ (C++17): This course focuses specifically on the C++17 Standard, which introduced critical robotics features like
std::optional,std::string_view, and structured bindings.In the world of Robotics, C++ is the undisputed king. It provides zero-cost abstractions—high-level features that compile into highly efficient machine code. When processing point clouds from a LIDAR at 10Hz or calculating inverse kinematics for a robot arm in under a millisecond, performance is critical.
Who Uses This In The Real World?
- Tesla Autopilot — Real-time sensor fusion and vehicle control systems.
- Open Robotics (ROS) — ROS 2 core is written in C++ for real-time distributed systems.
- Boston Dynamics — Control software for Atlas and Spot.
- NVIDIA Isaac — Accelerated robotics simulation and AI pipelines.
Before we write code, it's crucial to understand how your text becomes a robot action.
| Stage | Action | Intermediate Flag |
|---|---|---|
| 1. Preprocessor | Resolves #include and #define (textual copy-paste). |
g++ -E robot.cpp -o robot.i |
| 2. Compiler | Translates C++ to Assembly (CPU-specific logic). | g++ -S robot.cpp -o robot.s |
| 3. Assembler | Converts Assembly to Binary Machine Code (Object Files). | g++ -c robot.cpp -o robot.o |
| 4. Linker | Combines .o files and libraries into a final executable. |
g++ -o robot_init robot.o |
Let's write a simple program that outputs the status of a robot.
Code Example
#include <iostream>
int main() {
std::cout << "[Robot System] Initializing sensors... OK\n";
std::cout << "[Robot System] Ready for C++17 Robotics!\n";
return 0;
}
#include: A Preprocessor Directive. Think of it as a command to copy-paste. Before the compiler even sees your code, the preprocessor finds the file iostream and pastes its entire contents directly into your script.<iostream>: The C++ Standard Input/Output Stream library. It contains all the definitions needed for printing text to a screen or reading from a keyboard. (Without this, the compiler won't know what std::cout is!).int main(): The Entry Point. When the Operating System (like Linux/Ubuntu running on your robot) executes your program, it blindly searches for a function called main() to start running.int keyword signifies that this function must return an integer to the OS when it completes its run.std::cout: The Standard Character Output. std:: is the Namespace. A namespace is like a folder that groups all Standard C++ features together so their names don't clash with variables in your own code (e.g., if you had a custom variable also named cout).cout stands for character output (the console terminal).<<: The Stream Insertion Operator (or "Put" operator). It takes the text on its right and "pushes" it into the cout stream on its left to be printed.return 0;: This tells the Operating System that the program finished successfully. Any non-zero value (e.g., return 1; or return -1;) acts as an exit code signalling that an error or crash occurred.Compile and Run (All stages at once):
g++ -std=c++17 -Wall -Wextra -o robot_init robot.cpp
./robot_init
Bad Practice — Avoid
// Compiling without warnings — hides critical bugs in your robot's logic
g++ robot.cpp -o robot_init
// using namespace std; — pollutes the global namespace.
// Can lead to mysterious naming collisions in large ROS projects.
using namespace std;
cout << "Starting motors...";
// C-style printf — No type safety (can crash if types don't match)
int battery = 80;
printf("%s", battery);
Good Practice
#include <iostream>
int main() {
std::cout << "Starting motors...\n"; // Qualify with std::
// \n is faster than std::endl (endl forces the OS to "flush the buffer" and draw to the screen immediately, causing massive I/O delays in loops)
return 0;
}
Industry Best Practice
- Namespace Safety: Avoid
using namespace std;in headers or shared libraries. It pollutes the global namespace and causes naming collisions. In small.cppfiles or local examples, it is often acceptable for brevity.- Smart Output: Avoid
std::endlinside high-frequency sensor loops (e.g., 200Hz) because it forces a "buffer flush," causing massive I/O delays. Use\ninstead. Usestd::endlintentionally when you need to ensure a log message is written immediately (like right before a potential crash).- RAII Beyond Memory: C++ uses the RAII pattern for more than just memory. For example,
std::lock_guard(introduced later) automatically unlocks a mutex when the object goes out of scope, preventing deadlocks in multi-threaded robot nodes.- Strict Warnings: Always compile with
-std=c++17 -Wall -Wextra -Wconversion -Werror.- Use
clang-formatfor automatic formatting.- Enable AddressSanitizer from day one:
-fsanitize=address. It catches memory bugs instantly before they crash your actual robot.Warning
Every statement ends with
;— except lines with{ }. One missing semicolon can produce 20 error messages. Always fix the FIRST error and recompile.
g++: Build Systems (CMake)While compiling with g++ works for a single file, real robotics projects (like ROS packages) have hundreds of files. Instead of manually typing long compilation commands, the industry uses CMake.
CMake is a "meta-build system". You write a CMakeLists.txt file that describes your project, and CMake automatically generates the complex Makefiles for you.
A basic CMakeLists.txt for our robot program:
cmake_minimum_required(VERSION 3.16)
project(RobotProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(robot_init robot.cpp)
target_compile_options(robot_init PRIVATE -Wall -Wextra -Wconversion -Werror)
C++ compiler errors are notoriously terrifying to beginners, but they follow predictable patterns.
The Golden Rule of C++ Debugging
- Scroll to the VERY TOP of the error output.
- Fix the first error.
- Recompile.
- (Often, one missing semicolon causes 50 subsequent cascade errors. Do not try to fix error #50!).
**Commo
$ claude mcp add Robotics_CPP_Notes \
-- python -m otcore.mcp_server <graph>