Ubuntu24.04 G++ (C++ コンパイラ) をインストールする

Ubuntu24.04 G++ (C++ コンパイラ) をインストールする

Ubuntu24.04にG++ (C++ コンパイラ) をインストールするまでの手順を記述してます。

環境

  • OS Ubuntu24.04

node.jsインストール

まずは、Build-Essentialパッケージをインストールします。

$ sudo apt update && sudo apt install build-essentia

利用できるバージョンを確認してみます。

$ apt search '^g\+\+-[0-9]+$'

Sorting... Done
Full Text Search... Done
g++-10/noble 10.5.0-4ubuntu2 amd64
  GNU C++ compiler

g++-11/noble 11.4.0-9ubuntu1 amd64
  GNU C++ compiler

g++-12/noble 12.3.0-17ubuntu1 amd64
  GNU C++ compiler

g++-13/noble,now 13.2.0-23ubuntu4 amd64 [installed,automatic]
  GNU C++ compiler

g++-14/noble 14-20240412-0ubuntu1 amd64
  GNU C++ compiler

g++-9/noble 9.5.0-6ubuntu2 amd64
  GNU C++ compiler

インストールします。

$ sudo apt install g++-14

バージョンは以下で確認できます。

$ g++ --version

コンパイル

インストールが終わったので、一回実行してみます。以下の内容de「hello.cc」ファイルを作成して実行してます。

#include <iostream>
using namespace std;

int main() 
{
    cout << "Hello, World!" << endl;
    cout << "G++ version: " << __GNUC__ << "." << __GNUC_MINOR__ << "." << __GNUC_PATCHLEVEL__ << endl;
    return 0;
}

ファイルを作成します。

$ nano hello.cc

コンパイルして実行します。

$ g++ -o hello hello.cc

$ ./hello
Hello, World!
G++ version: 13.2.0

正常に実行されていることが確認できます。