如何在Ubuntu上安装GCC

时间:2020-03-05 15:31:57  来源:igfitidea点击:

GCC是一个代表GNU编译集的首字母缩写,是一个用于编程语言的编译器集合,包括Java,C和C++。
在本教程中,我们将向我们展示如何在Ubuntu和Debian中安装GCC编译器和相关工具

使用APT安装GCC编译器

安装GCC编译器的第一步是更新和升级系统。

这可以在终端上的单个命令中完成,如图所示

# sudo apt update && sudo apt upgrade

接下来,安装Build-Essential包。
构建基本程序包包含一组包,这些包被认为是构建Ubuntu包的关键。
这包括GCC编译器,制作和其他基本工具。

安装Build-Essential包运行

# sudo apt install build-essential

示例输出

Reading package lists... Done
Building dependency tree
Reading state information... Done
build-essential is already the newest version.

验证安装

验证我们已安装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 5.4.0-6ubuntu1~16.04.10) 5.4.0 20150609
Copyright (C) 2014 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运行的版本

make -v

或者

make --version

输出

GNU Make 3.81
Copyright (C) 2006  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.
This program built for x86_64-pc-linux-gnu

接下来,我们将在Ubuntu/Debian发行版上安装Dev Man页面

安装Dev Man页面

要安装dev man页面运行以下命令

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

示例输出

Reading package lists... Done
Building dependency tree
Reading state information... Done
manpages-dev is already the newest version.
manpages-dev set to manually installed.
man-db is already the newest version.
The following extra packages will be installed:
  manpages-posix
The following NEW packages will be installed:
  manpages-posix manpages-posix-dev
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,291 kB of archives.
After this operation, 6,459 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

要查看库调用,请运行以下命令

# man 3 scanf
# man 2 execve
# man 2 fork

现在我们已确认成功安装编译器和主要组件,让我们测试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

最后,运行

./test

输出

Hello world !