Hit:1 http://mirrors.cloud.aliyuncs.com/ubuntu jammy InRelease
Hit:2 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-updates InRelease
Hit:3 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-backports InRelease
Hit:4 http://mirrors.cloud.aliyuncs.com/ubuntu jammy-security InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
157 packages can be upgraded. Run 'apt list --upgradable' to see them.
次に、GCC コンパイラと関連する開発ツールをインストールします:
sudo apt install -y build-essential
実行結果の例:
Reading package lists...
Building dependency tree...
Reading state information...
build-essential is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 22 not upgraded.
インストールを確認し、GCC のバージョンを確認するには、次のコマンドを実行します:
g++ --version
実行結果の例:
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
このステップでは、.cpp拡張子を持つ新しい C++ ソースファイルを作成する方法を学びます。C++ では、ソースファイルは C++ コードを含むことを示すために.cpp拡張子を使用します。最初の C++ ソースファイルを作成するために、WebIDE のファイルエクスプローラーとテキストエディタを使用します。
このステップでは、C++ における一般的な構文エラーについて学びます。特に、セミコロンの欠落に焦点を当てます。セミコロンは C++ において重要で、文の終わりを示します。
WebIDE でsyntax_errors.cppという新しいファイルを作成します:
cd ~/project
touch syntax_errors.cpp
#include <iostream>
int main() {
// 一般的な構文エラー:セミコロンの欠落
std::cout << "This line has an error" // ここにセミコロンが欠落しています!
std::cout << "This line will not compile" << std::endl;
// セミコロンを含む正しいバージョン
std::cout << "This line is correct" << std::endl;
std::cout << "All statements end with a semicolon" << std::endl;
return 0; // ここもセミコロンを忘れないでください!
}
プログラムをコンパイルしてみましょう:
g++ syntax_errors.cpp -o syntax_errors
コンパイルエラーの例:
syntax_errors.cpp: In function ‘int main()’:
syntax_errors.cpp:5:42: error: expected ‘;’ before ‘std’
5 | std::cout << "This line has an error" // Missing semicolon here!
| ^
| ;
6 | std::cout << "This line will not compile" << std::endl;
| ~~~