Search

How to Install Carbon Language in Ubuntu and First Steps

Carbon is the new programming language from Google, Open Source that intends to be the successor to C++.


How to Install Carbon Language in Ubuntu and First Steps


Carbon, or Carbon-Lang, is an experimental, general-purpose programming language.

The project is open-source and was started by Google, following in the footsteps of previous Google-made programming languages (Go and Dart). Google engineer Chandler Carruth first introduced Carbon at the CppNorth conference in Toronto in July 2022. He stated that Carbon was created to be a C++ successor.

The language is expected for a 1.0 release to occur in 2024 or 2025. The language intends to fix several perceived shortcomings of C++ but otherwise provides a similar feature set.

The main goals of the language are readability and “bi-directional interoperability”, as opposed to using a new language like Rust (which, while being based on C++, is not two-way compatible with C++ programs). Changes to the language will be decided by the Carbon leads.

Carbon’s documents, design, implementation, and related tools are hosted on GitHub under the Apache-2.0 license with LLVM Exception.

In this article we will see how to install Carbon in Ubuntu and the first steps.


Installation

01. Update the system:

sudo apt update

02. Install the necessary dependencies:

sudo apt install sudo apt install build-essential curl git zlib1g-dev

03. Install Homebrew:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

04. Load the Homebrew environment:

eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"

05. Install packages with Homebrew:

brew install gcc python@3.9 bazelisk llvm

06. Update PIP and add llvm to the $PATH variable:

pip3.9 install -U pip
export PATH="$(brew --prefix llvm)/bin:${PATH}"

07. Clone and run Carbon:

git clone https://github.com/carbon-language/carbon-lang
cd carbon-lang
bazel run //explorer -- ./explorer/testdata/print/format_only.carbon

First steps

08. ‘Hello, World!’ in carbon

vim ./explorer/testdata/prefix/sandbox.carbon

package sample api;

fn Main() -> i32 {
  Print("Hello, World!");
  return 0;
}

To compile, run: bazel run //explorer -- ./explorer/testdata/print/sandbox.carbon

09. Declaring variables:

package sample api;

fn Main() -> i32 {
  var s: auto = "Hello, Carbon";
  Print(s);
  return 0;
}

10. Basics for if..else conditions

package sample api;

fn Main() -> i32 {
    let x: i32 = 42;
    if( x == 6 ){
      Print("x is not equal to 6!");
    }else{
      Print("x is equal to {0}", x);
    }
    return 0;
}

11. Creating functions without parameters:

package sample api;

fn helloWorld() -> String {
  return "Hello world via function!";
}

fn Main() -> i32 {
  Print( helloWorld() );
  return 0;
}

12. Declaring functions with parameters

package sample api;

fn mySum(var x: i32, var y: i32) -> i32 {
  return x + y;
}

fn Main() -> i32 {
  Print("The sum of x + y: {0}", mySum(9, 3) );
  return 0;
}

For more information see the examples in the explorer/testdata directory and the documentation.

If you want, try using Carbon online at: https://carbon.godbolt.org/.


carbon cpp programming


Share



Comments