在Windows子系统上安装g++

13

前段时间我在我的电脑上启用了Windows子系统Linux,但没有多少使用。现在我有一个想法,那就是在WSL上安装gcc/++ 7并一直遇到问题。

我的想法是通过下载它,并按照这个指南的方法手动编译:

../gcc/configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --prefix=${HOME}/software/gcc-7.3.0/installDir --enable-checking=release --enable-languages=c,c++,fortran --disable-multilib

这导致了以下错误:

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking LIBRARY_PATH variable... ok
checking GCC_EXEC_PREFIX variable... ok
checking whether to place generated files in the source directory... no
checking whether a default linker was specified... no
checking whether a default assembler was specified... no
checking for x86_64-linux-gnu-gcc... no
checking for gcc... no
checking for x86_64-linux-gnu-cc... no
checking for cc... no
checking for x86_64-linux-gnu-cl.exe... no
checking for cl.exe... no
configure: error: in `$HOME/software/gcc-7.3.0/build':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.

显然,没有安装gcc。至少gcccc命令都提示“未找到命令”。因此,接下来我通过以下方式安装了gcc:

sudo apt install gcc

这个有效:

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.4) 4.8.4 
Copyright (C) 2013 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.

然而,当我尝试上述的配置命令时,出现了以下错误:

checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu 
checking LIBRARY_PATH variable... ok
checking GCC_EXEC_PREFIX variable... ok
checking whether to place generated files in the source directory... no
checking whether a default linker was specified... no
checking whether a default assembler was specified... no
checking for x86_64-linux-gnu-gcc... x86_64-linux-gnu-gcc
checking for C compiler default output file name...
configure: error: in `/home/seriously-ubuntu/software/gcc-7.3.0/build':
configure: error: C compiler cannot create executables
see `config.log' for more details.

于是我试图手动编译一个简单的C程序:

int main() {
    return 42;
}

这表明某些东西似乎严重出错了。

$ gcc foo.c
/usr/bin/ld: cannot find crt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: cannot find -lc
/usr/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status

有什么建议可以帮助我修复这个问题吗?如果重新设置整个WSL有所帮助,我也不介意 :/

lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial

更新:

在进行以下操作之前,我尝试使用apt安装gcc7 / g ++,但未成功:

$ sudo add-apt-repository ppa:jonathonf/gcc-7.1
$ sudo apt-get update
$ sudo apt-get install gcc-7 g++-7
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++-7 : Depends: libstdc++-7-dev (= 7.1.0-10ubuntu1~16.04.york0) but it is not going to be installed
        Depends: libisl15 (>= 0.15) but it is not installable 
gcc-7 : Depends: cpp-7 (= 7.1.0-10ubuntu1~16.04.york0) but it is not going to be installed
        Depends: binutils (>= 2.26.1) but 2.24-5ubuntu14.2 is to be installed
        Depends: libisl15 (>= 0.15) but it is not installable
        Recommends: libc6-dev (>= 2.13-0ubuntu6) but it is not going to be installed
E: Unable to correct problems, you have held broken packages

更新2

遗憾的是,没有任何建议的方法能够解决问题,我最终选择强制重置WSL。显然,有更严重的故障发生了。


我建议从头重新安装WSL,除非你愿意清除操作系统并重新安装,否则永远不要从源代码安装gcc! - n. m.
4个回答

14

为什么要编译?您应该能够安装软件包:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt update
sudo apt install g++-7 -y

使用以下方式进行验证:

gcc-7 --version

请查看如何安装gcc-7或clang 4.0?


1
当然,您是正确的,我应该提到这是我尝试过的第一件事,但我遇到了依赖地狱。请参见更新的问题。 - Seriously
我已经添加了另一个ppa。我刚刚在从Windows 10 x64下的Microsoft Store安装的新的Ubuntu 16.04上进行了验证,这很好用。 - Suma
第一条命令是做什么的?在我的情况下它没有起作用,但是 g++-7 --version 报告说它已经安装了,还有 gcc-7 --version - Denis G. Labrecque
我认为它添加了另一个存储库,可以用来获取安装文件。虽然我不太理解apt。 - Suma
那确实安装了“gcc-7”,但我真的需要一个“g ++”二进制文件。之后我运行了apt install g ++,它就可以工作了。 - bvdb

6
运行以下命令:
sudo apt install aptitude
sudo aptitude install gcc-7 g++-7

或者

sudo apt install aptitude && sudo aptitude install golang gcc-7 g++-7

1

crt1.o通常作为libdevc依赖的一部分提供(或类似的内容)。我建议运行sudo apt search libc或某个类似变体,使用lib6clibdevlibc-dev等进行安装,这些安装可以解决我最近遇到的类似问题。

如果失败,运行find / -iname ctri.o并将其出现的文件夹添加到您的PATH中,使用export PATH="[folder]:$PATH",看看是否有所帮助。


我不知道crt1.olibdevclib6clibdevlibc-dev是什么,也不知道export PATH="[folder]:$PATH"的作用,以及它们与安装g++有什么关系。对我来说这些都是无意义的废话。 - Denis G. Labrecque

0

正如您可以在这里看到的那样,g++只是默认Ubuntu软件包存储库中的一个软件包(适用于Ubuntu 18.04、20.04和22.04等版本)。

因此,只需运行此命令即可。

sudo apt update
sudo apt install g++

之后,您可以使用以下方式进行检查:

g++ --version

这导致了在撰写本文时的版本9.4.0。

但是,您也可以选择特定的版本(例如apt install g++-12)。然而,二进制文件也将具有特定的名称(例如g++-12),如果需要它成为g++,则可能需要添加符号链接(例如ln -s /usr/bin/g++-12 /usr/bin/g++)。


正如你从我的问题中看到的,我实际上已经尝试过这个 :) 但是,在撰写问题时,特定版本的GCC-7还没有包,因为它只发布了5个月。问题在于,在某个地方系统崩溃了。 - Seriously

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接