在 Linux 上運行 C++ 程序,你需要先確保已經安裝了 g++ 編譯器
hello_world.cpp
,并編寫以下代碼:#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
保存文件并關閉編輯器。
打開終端(Terminal),然后使用 cd
命令導航到包含 hello_world.cpp
文件的目錄。例如:
cd /path/to/your/directory
g++ hello_world.cpp -o hello_world
這將生成一個名為 hello_world
的可執行文件(在 Windows 上為 hello_world.exe
)。
./hello_world
注意:在運行程序之前,請確保你的系統已經安裝了 g++ 編譯器。如果尚未安裝,可以使用以下命令安裝:
sudo apt-get update
sudo apt-get install g++ build-essential
sudo dnf install gcc-c++ make
sudo yum install gcc-c++ make