如何在 Ubuntu 上安裝 GCC

GCC,縮寫,代表 GNU編譯器 Collection,是用於編程語言的編譯器的集合,包括 C、C++、Objective-C、Fortran、Ada、Go 和 D。

在本指南中,我們將學習如何 安裝 GCCUbuntu 20.04.

第一步:更新系統

首先,確保更新您的 Ubuntu 系統。

$ sudo apt update

第 2 步:在 Ubuntu 上安裝 GCC

Ubuntu 默認存儲庫包含一個名為 build-essential 的元包。 該軟件包包含 GNU 調試器、g++/GNU 編譯器等必備工具的集合。

要在 Ubuntu 上安裝 build-essential 包,請鍵入:

$ sudo apt install build-essential

步驟 3:驗證 GCC 安裝

要驗證您是否已安裝 gcc 並運行以下命令

$ whereis gcc make

輸出

gcc: /usr/bin/gcc /usr/lib/gcc /usr/share/man/man1/gcc.1.gz
make: /usr/bin/make /usr/share/man/man1/make.1posix.gz /usr/share/man/man1/make.1.gz

或者,您可以運行

$ gcc --version

輸出

gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 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.

獲取 make run 的版本

make -v

或者

make --version

接下來,我們將在 Ubuntu 發行版上安裝開發手冊頁

安裝開發手冊頁

要安裝開發手冊頁,請運行以下命令

sudo apt install manpages-dev man-db manpages-posix-dev

要查看庫調用,請運行以下命令

# man 3 scanf

# man 2 execve

安裝 GCC 編譯器

# man 2 fork

安裝 GCC 編譯器

現在我們已經確認編譯器和主要組件安裝成功,讓我們測試一下 GNU GCC 編譯器

測試 GCC 編譯器

讓我們首先創建一個簡單的 C++ 程序

vim test_app.cpp

添加以下代碼

// Simple C++ program to display "Hello World"

// Header file for input output functions
#include

using namespace std;

// main function -
// where the execution of program begins
int main()
{
// prints hello world
cout<<"Hello World ! n";

return 0;
}

接下來編譯代碼如下

g++ test_app.cpp -o test

請務必使用 ls 命令在您的目錄中找到名為 test 的文件。

ls -l

安裝 gcc 編譯器

最後,如圖所示運行

./test

輸出

Hello world !

結論

我們已經在 Ubuntu 上成功安裝了 GCC 並編譯了一個 Hello World example.

感謝閱讀,請在評論區提出您的建議和反饋。