LoginSignup
0
0

More than 1 year has passed since last update.

Googleが発表した新言語、CarbonでHello, World!

Posted at

Carbonとは

Googleが発表した新しいプログラミング言語。C++の後継を目指しているそうです。

環境構築

今回はUbuntu(WSL2)でCarbonを動かしてみます。公式の手順にはHomebrewで必要ツール群を導入する手順が書かれているため、まずはHomebrewを導入します。以下のリンク先にあるインストールコマンドを実行して、Homebrewをインストールしておきましょう。

インストール後、brewコマンドが使えるようにパスを追加しておく必要があります。以下のコードを~/.bash_profileにでも追記しておきます。

~/.bash_profile
# Homebrew
export PATH=/home/linuxbrew/.linuxbrew/bin:$PATH
# ~/.bash_profileの再読み込み
$ source ~/.bash_profile

ここまで行えば、brewコマンドが使えるようになっているはずです。brewコマンドで必要なツールをインストールします。

$ brew install bazelisk
$ brew install llvm

ここまでできればツールの準備は完了。最後にCarbon言語を実行できるように、リポジトリをクローンしておきましょう。

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

Hello, World!

以下のコードを、hello.carbonとでも名前を付けて用意します。
(今回は例としてホームディレクトリにhello.carbonを作成しています。)

~/hello.carbon
package hello api;

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

用意ができたら、先ほどクローンしたCarbonのディレクトリで以下のコマンドを実行してみましょう。

$ bazel run //explorer -- ~/hello.carbon
~(省略)~
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Hello, World!
result: 0

Hello, World!と表示されていれば成功です。

最後に

C++の後継と言われるとRustをイメージされる方が多いと思いますが、C++からRustへの移行は実際のところ容易なものではありません。C++の後継を意識して作られているCarbon言語、今はまだマイナーかもしれませんが、10年後にはC++の後継言語としてメジャーな言語となっているかもしれません。今後の動向が楽しみです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0