Ubuntu gcc problem: fatal error: stdio.h: No such file or directory

2021-12-22
#Unix #C

1. 简介

gcc in Ubuntu is not installed by default and needs to be installed:

sudo apt-get install gcc

However, after installing gcc, there is still such error: fatal error: stdio.h: No such file or directory

Installation required:

$ sudo apt-get install build-essential
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package build-essential
$ sudo apt-get install libc6-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package libc6-dev is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'libc6-dev' has no installation candidate

2. Steps

  1. Change the source first and selectSoftware & Updates, choose China mirrors.aliyun.com. Note that it depends on your location.

  2. Update system

$ sudo apt-get update
Hit:1 http://mirrors.aliyun.com/ubuntu focal InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu focal-updates InRelease                                                     
Hit:3 http://mirrors.aliyun.com/ubuntu focal-backports InRelease                                                   
Hit:4 http://mirrors.aliyun.com/ubuntu focal-security InRelease                                                    
Hit:5 http://download.zerotier.com/debian/bionic bionic InRelease                                                  
Hit:6 http://archive.canonical.com/ubuntu focal InRelease           
Hit:7 http://archive.ubuntu.com/ubuntu focal InRelease
Reading package lists... Done
$ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
$ sudo apt-get install libc6-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 libc6-dev : Depends: libc6 (= 2.31-0ubuntu9) but 2.31-0ubuntu9.2 is to be installed
E: Unable to correct problems, you have held broken packages.
$ sudo apt-get install libc6=2.31-0ubuntu9
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Suggested packages:
  glibc-doc
The following packages will be REMOVED:
  libc6-dbg
The following packages will be DOWNGRADED:
  libc6
0 upgraded, 0 newly installed, 1 downgraded, 1 to remove and 0 not upgraded.
Need to get 2,713 kB of archives.
After this operation, 76.4 MB disk space will be freed.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libc6 amd64 2.31-0ubuntu9 [2,713 kB]
Fetched 2,713 kB in 2s (1,414 kB/s)
Preconfiguring packages ...
(Reading database ... 375751 files and directories currently installed.)
Removing libc6-dbg:amd64 (2.31-0ubuntu9.2) ...
dpkg: warning: downgrading libc6:amd64 from 2.31-0ubuntu9.2 to 2.31-0ubuntu9
(Reading database ... 375437 files and directories currently installed.)
Preparing to unpack .../libc6_2.31-0ubuntu9_amd64.deb ...
Unpacking libc6:amd64 (2.31-0ubuntu9) over (2.31-0ubuntu9.2) ...
Setting up libc6:amd64 (2.31-0ubuntu9) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
$ sudo apt-get install libc6-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libc-dev-bin libcrypt-dev linux-libc-dev manpages-dev
Suggested packages:
  glibc-doc
The following NEW packages will be installed:
  libc-dev-bin libc6-dev libcrypt-dev linux-libc-dev manpages-dev
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 6,068 kB of archives.
After this operation, 30.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libc-dev-bin amd64 2.31-0ubuntu9 [71.8 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 linux-libc-dev amd64 5.4.0-26.30 [1,106 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 libcrypt-dev amd64 1:4.4.10-10ubuntu4 [104 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 libc6-dev amd64 2.31-0ubuntu9 [2,520 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/main amd64 manpages-dev all 5.05-1 [2,266 kB]                         
Fetched 6,068 kB in 12s (486 kB/s)                                                                                 
Selecting previously unselected package libc-dev-bin.
(Reading database ... 375437 files and directories currently installed.)
Preparing to unpack .../libc-dev-bin_2.31-0ubuntu9_amd64.deb ...
Unpacking libc-dev-bin (2.31-0ubuntu9) ...
Selecting previously unselected package linux-libc-dev:amd64.
Preparing to unpack .../linux-libc-dev_5.4.0-26.30_amd64.deb ...
Unpacking linux-libc-dev:amd64 (5.4.0-26.30) ...
Selecting previously unselected package libcrypt-dev:amd64.
Preparing to unpack .../libcrypt-dev_1%3a4.4.10-10ubuntu4_amd64.deb ...
Unpacking libcrypt-dev:amd64 (1:4.4.10-10ubuntu4) ...
Selecting previously unselected package libc6-dev:amd64.
Preparing to unpack .../libc6-dev_2.31-0ubuntu9_amd64.deb ...
Unpacking libc6-dev:amd64 (2.31-0ubuntu9) ...
Selecting previously unselected package manpages-dev.
Preparing to unpack .../manpages-dev_5.05-1_all.deb ...
Unpacking manpages-dev (5.05-1) ...
Setting up manpages-dev (5.05-1) ...
Setting up linux-libc-dev:amd64 (5.4.0-26.30) ...
Setting up libcrypt-dev:amd64 (1:4.4.10-10ubuntu4) ...
Setting up libc-dev-bin (2.31-0ubuntu9) ...
Setting up libc6-dev:amd64 (2.31-0ubuntu9) ...
Processing triggers for man-db (2.9.1-1) ...

3. Summary

Summarize key points:

  • Change the source, preferably commercial;
  • libc6-dev is the key to stdio.h.

4. Reference