Getting Started With Carbon Language

Carbon is an experimental programming language being developed by Google as a potential long-term successor to C++.

This guide will cover how to get started with Carbon. Carbon’s Github repo is an essential resource for learning about, working with, and keeping up to date with Carbon.

Currently there is no compiler for Carbon. There are currently two ways to experiment with it:

  1. Use the Carbon Explorer – the easiest way to play with Carbon is to use the carbon explorer at: http://carbon.compiler-explorer.com
  2. Build the Carbon explorer yourself using Homebrew and Bazel. The steps are as follows:

Step 1: Install Homebrew

Homebrew is a package manager that we will need in order to install a number of helpful tools.

You can check if Homebrew is already installed:

brew –version

If you’re using a Windows machine, make sure you’re using WSL (instructions below).

Follow the instructions below to install Homebrew.

Linux and MacOS:

1. Follow the instructions on Homebrew’s homepage.

Windows:

You will need WSL (Windows Subsystem for Linux) installed. WSL allows you to run a GNU/Linux environment directly on Windows. Homebrew recommends WSL 2.

1. Follow Microsoft’s WSL installation instructions.

2. Install Homebrew via WSL using the instructions on Homebrew’s homepage.

Step 2: Install Bazel

Bazel is an open-source build and test tool that supports large codebases with multi-platform support. Bazel was chosen as Carbon’s build system, and is recommended to install via bazelisk.

brew install bazelisk

Step 3: Install LLVM

LLVM is “a collection of modular and reusable compiler and toolchain technologies”. Is is not, as its name suggests, a type of virtual machine.

We will be using LLVM to help compile Carbon; first we need to install it:

brew install llvm

Step 4: Git Clone the carbon-lang Repo

Next we will need to clone the carbon-lang repository from GitHub:

git clone https://github.com/carbon-language/carbon-lang

If you need to install git, you can follow the instructions here.

Step 5: Use Bazel To Run The Explorer

The following command will run the code at the location ‘./explorer/testdata/print/format_only.carbon’, which contains code for a simple ‘Hello World’ function.

bazel run //explorer — ./explorer/testdata/print/format_only.carbon