Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla CppClient Example

From Leeroopedia
Knowledge Sources
Domains C++ Client, Example Scripts
Last Updated 2026-02-15 05:00 GMT

Overview

A C++ example demonstrating how to use the CARLA C++ client library (libcarla) to connect to a server, load a map, spawn a vehicle, and control it.

Description

This example demonstrates the core CARLA C++ client workflow. It includes headers for Client, World, Map, BlueprintLibrary, Sensor, ActorBlueprint, and Transform. The ParseArguments function accepts optional host and port (defaulting to localhost:2000). The RandomChoice template selects random elements from ranges. The main function: (1) connects to the server with a 40-second timeout and prints client/server API versions, (2) loads a random town from available maps, (3) retrieves the blueprint library and filters for vehicles, (4) randomizes the vehicle color attribute if available, (5) gets a random spawn point from the map's recommended spawn points, (6) spawns the vehicle and applies full throttle control, (7) repositions the spectator camera behind and above the vehicle for viewing, and (8) destroys the vehicle on completion. Error handling catches TimeoutException and general exceptions. A commented-out section demonstrates semantic segmentation camera setup, callback registration, and image saving using ImageIO and ColorConverter::CityScapesPalette.

Usage

Use this example as a starting point for C++ applications that need to interact with the CARLA simulator, such as custom planners, sensor processing pipelines, or automated testing frameworks that require the performance of native C++ code.

Code Reference

Source Location

  • Repository: CARLA
  • File: Examples/CppClient/main.cpp

Signature

static auto ParseArguments(int argc, const char *argv[]);
int main(int argc, const char *argv[]);

// Namespace aliases used:
namespace cc = carla::client;
namespace cg = carla::geom;
namespace csd = carla::sensor::data;

Import

#include <carla/client/Client.h>
#include <carla/client/World.h>
#include <carla/client/BlueprintLibrary.h>
#include <carla/client/Map.h>
#include <carla/client/Sensor.h>
#include <carla/geom/Transform.h>

I/O Contract

Inputs

Name Type Required Description
host std::string No CARLA server hostname (default: localhost)
port uint16_t No CARLA server port (default: 2000)

Outputs

Name Type Description
Console output stdout Client/server versions, spawned actor ID, status messages
Return code int 0 on success, 1 on timeout, 2 on other exceptions

Usage Examples

// Build and run:
// mkdir build && cd build && cmake .. && make
// ./cpp_client localhost 2000

#include <carla/client/Client.h>
#include <carla/client/World.h>

int main() {
    auto client = cc::Client("localhost", 2000);
    client.SetTimeout(40s);

    // Load a world
    auto world = client.LoadWorld("Town03");

    // Get blueprint library and spawn a vehicle
    auto bp_library = world.GetBlueprintLibrary();
    auto vehicles = bp_library->Filter("vehicle");
    auto blueprint = (*vehicles)[0];

    auto map = world.GetMap();
    auto transform = map->GetRecommendedSpawnPoints()[0];
    auto actor = world.SpawnActor(blueprint, transform);

    // Apply control
    auto vehicle = std::static_pointer_cast<cc::Vehicle>(actor);
    cc::Vehicle::Control control;
    control.throttle = 1.0f;
    vehicle->ApplyControl(control);

    // Cleanup
    vehicle->Destroy();
    return 0;
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment