一、在ubuntu中安装gcc和g++编译
sudo apt install gcc
sudo apt install g++
二、失败,更换源
sudo apt update报错
命中:1 http://packages.microsoft.com/repos/vscode stable InRelease
命中:2 http://ppa.launchpad.net/elementary-os/stable/ubuntu bionic InRelease
获取:3 http://packages.elementary.io/appcenter bionic InRelease [5,894 B]
命中:4 http://ppa.launchpad.net/elementary-os/os-patches/ubuntu bionic InRelease
获取:5 http://packages.elementary.io/appcenter bionic/main amd64 DEP-11 Metadata [93.6 kB]
命中:6 http://security.ubuntu.com/ubuntu bionic-security InRelease
错误:7 http://cn.archive.ubuntu.com/ubuntu bionic InRelease
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::19) 的连接 - connect (101: 网络不可达)
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::16) 的连接 - connect (101: 网络不可达)
无法连接上 cn.archive.ubuntu.com:80 (91.189.91.26),连接超时 无法连接上 cn.archive.ubuntu.com:80 (91.189.91.23),连接超时 [IP: 91.189.91.23 80]
错误:8 http://cn.archive.ubuntu.com/ubuntu bionic-updates InRelease
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::19) 的连接 - connect (101: 网络不可达)
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::16) 的连接 - connect (101: 网络不可达) [IP: 91.189.91.23 80]
错误:9 http://cn.archive.ubuntu.com/ubuntu bionic-backports InRelease
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::19) 的连接 - connect (101: 网络不可达)
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::16) 的连接 - connect (101: 网络不可达) [IP: 91.189.91.23 80] 已下载 99.5 kB,耗时 1分 4秒 (1,552 B/s)
正在读取软件包列表… 完成 正在分析软件包的依赖关系树
正在读取状态信息… 完成
有 3 个软件包可以升级。请执行 ‘apt list --upgradable’ 来查看它们。
W: 无法下载 http://cn.archive.ubuntu.com/ubuntu/dists/bionic/InRelease 无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::19) 的连接 - connect (101: 网络不可达)
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::16) 的连接 - connect (101: 网络不可达)
无法连接上 cn.archive.ubuntu.com:80 (91.189.91.26),连接超时 无法连接上 cn.archive.ubuntu.com:80 (91.189.91.23),连接超时 [IP: 91.189.91.23 80]
W: 无法下载 http://cn.archive.ubuntu.com/ubuntu/dists/bionic-updates/InRelease
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::19) 的连接 - connect (101: 网络不可达)
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::16) 的连接 - connect (101: 网络不可达) [IP: 91.189.91.23 80]
W: 无法下载 http://cn.archive.ubuntu.com/ubuntu/dists/bionic-backports/InRelease
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::19) 的连接 - connect (101: 网络不可达)
无法发起与 cn.archive.ubuntu.com:80 (2001:67c:1562::16) 的连接 - connect (101: 网络不可达) [IP: 91.189.91.23 80]
W: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。
这是因为cn.archive.ubuntu.com被墙了,无法连接,
需要切换到国内源.
首先备份源列表:
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup
而后用gedit或其他编辑器打开:
sudo gedit /etc/apt/sources.list
#添加阿里源
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
更新
sudo apt-get update
sudo apt-get upgrade
安装
sudo apt-get build-dep gcc
测试
gcc --version
输出
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright © 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.
三、Ubuntu 我的第一个C语言文件
#include<stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}