在Ubuntu中安装C++编译器非常简单,因为Ubuntu默认已经安装了GCC(GNU编译器集合),其中包括C++编译器g++。以下是安装步骤:
打开终端(快捷键:Ctrl + Alt + T)
首先,更新软件包列表以确保您获得最新版本:
sudo apt update
sudo apt install g++
g++ --version
现在,您已经成功在Ubuntu中安装了C++编译器。您可以开始编写C++代码并使用g++进行编译和运行。
例如,创建一个名为hello.cpp
的文件,其中包含以下C++代码:
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
保存文件后,在终端中运行以下命令以编译代码:
g++ hello.cpp -o hello
这将生成一个名为hello
的可执行文件。要运行此文件,请在终端中输入:
./hello
您将看到输出 “Hello, World!”。