在 Ubuntu 上安装 G++ 时的错误和问题

在 Ubuntu 上安装 G++ 时的错误和问题

尝试安装 g++ 时遇到此错误:

$ sudo apt-get install g++
[sudo] password for eisha: 
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:
 g++ : Depends: g++-9 (>= 9.3.0-3~) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

以下是该命令的输出$ apt policy g++

$ apt policy g++
g++:
  Installed: (none)
  Candidate: 4:9.3.0-1ubuntu2
  Version table:
     4:9.3.0-1ubuntu2 500
        500 http://us.archive.ubuntu.com/ubuntu focal/main amd64 Packages

以下是该命令的输出$ apt policy g++-9

$ apt policy g++-9
g++-9:
  Installed: (none)
  Candidate: 9.3.0-17ubuntu1~20.04
  Version table:
     9.3.0-17ubuntu1~20.04 500
        500 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages
     9.3.0-10ubuntu2 500
        500 http://us.archive.ubuntu.com/ubuntu focal/main amd64 Packages

是的,我确实尝试过:sudo apt install build-essential并且输出如下:

sudo apt install build-essential
[sudo] password for eisha: 
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:
 build-essential : Depends: libc6-dev but it is not going to be installed or
                            libc-dev
                   Depends: g++ (>= 4:9.2) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

是的,我也尝试过,sudo apt update 我得到了一个很长的输出,结尾几行是:

Last modification reported: Wed, 26 Jan 2022 19:25:00 +0000
   Release file created at: Wed, 26 Jan 2022 16:17:36 +0000
W: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/focal-updates/restricted/i18n/Translation-en.xz  
E: Failed to fetch http://us.archive.ubuntu.com/ubuntu/dists/focal-updates/universe/binary-amd64/Packages.xz  
W: Some index files failed to download. They have been ignored, or old ones used instead

答案1

总结:降级libc6libc6=2.31-0ubuntu9.2并安装build-essential

您没有启用所需的存储库。此外,软件包的版本已损坏。

apt-cache policy g++通过在 Ubuntu 20.04 上运行,我得到:

g++:
  Installed: (none)
  Candidate: 4:9.3.0-1ubuntu2
  Version table:
     4:9.3.0-1ubuntu2 500
        500 http://mirrors.my mirror.com/ubuntu focal/main amd64 Packages

因此,您必须启用main存储库。
运行:

sudo add-apt-repository main && sudo apt update

启用main存储库后,安装正确版本的libc6

sudo apt install --reinstall libc6=2.31-0ubuntu9.2

不要跑apt upgrade

现在,更新并清理:

sudo apt update
sudo apt clean
sudo apt autoremove
sudo apt -f install

现在,安装元包,build-essential以安装g++

sudo apt install aptitude
sudo aptitude -f install build-essential

如果上述方法不起作用,请尝试恢复默认存储库并使用aptitude安装 build-essential:

sudo mkdir ~/answer && cd ~/answer/ && sudo wget https://gist.githubusercontent.com/ishad0w/788555191c7037e249a439542c53e170/raw/3822ba49241e6fd851ca1c1cbcc4d7e87382f484/sources.list && sudo sed -i "s/focal/$(lsb_release -c -s)/" ~/answer/sources.list && sudo mv ~/answer/sources.list /etc/apt/ && sudo apt update && sudo apt upgrade  && sudo rm -rf ~/answer
sudo aptitude -f install build-essential

还是没用

编辑您的问题以包括:

  • sudo apt install libc6-dev
  • sudo apt update
  • apt-cache policy libc6
  • apt-cache policy libc-dev

添加这些详细信息后请 ping 我;)

答案2

根据此文档尝试以下操作

首先,更新包索引sudo apt update

在 Ubuntu 20.04 上安装 C++ 编译器的推荐方法是安装整个开发包build-essential

要安装,请运行以下命令:

$ sudo apt install build-essential

然后检查是否已成功安装:

$ g++ --version

您应该获得类似以下的输出:

$ g++ --version
gcc (Ubuntu 9.2.1-17ubuntu1) 9.2.1 20191102

相关内容