問題
cargo run や cargo test を実行した際に、以下のエラーが発生します。
$ cargo run
Compiling sample v0.1.0 (/mnt/c/rust_project/sample)
error: linker `cc` not found
|
= note: No such file or directory (os error 2)
error: could not compile `sample` (bin "sample") due to 1 previous error
このエラーは、Rust のビルドに必要な Cコンパイラ(cc) がシステムに存在しない場合に発生します。
原因
cargo run や cargo test 実行時、Rust コンパイラ(rustc)は内部的に Cコンパイラを呼び出すことがあります。しかし、Ubuntu 環境では初期状態で Cコンパイラがインストールされていないため、cc が見つからずエラーになります。
対応方法
build-essential パッケージをインストールして Cコンパイラや関連ツールを追加します。
sudo apt update
sudo apt install -y build-essential
インストール後、再度 cargo run または cargo test を実行してエラーが解消されたことを確認します。
動作環境
- cargo 1.89.0
- Ubuntu 24.04.3 LTS