Installing g++ on windows subsystem for linux
Asked Answered
M

4

13

A while back I activated Windows Subsystem for Linux on my machine but didn't use it much. Now I have an idea what I could use it for and that is why I'm trying to install gcc/++ 7 on my WSL and keep running into problems.

My idea was to download it and compile it by hand following this guide with:

../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

This led to the following error:

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.

Apparently, there was no(?) gcc installed at all. At least gcc and cc yielded "command not found". So my next step was installing gcc via:

sudo apt install gcc

This worked:

$ 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.

However when trying the configure command from above I got:

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.

So I tried to compile a simple c program by hand:

int main() {
    return 42;
}

Which hints that something seems to be seriously broken

$ 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

Any Ideas where I can even start to repair this? I wouldn't even mind reseting the whole WSL if this helps :/

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

UPDATE:

Before all of this I tried installing gcc7 / g++/ via apt without success:

$ 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

UPDATE 2

Sadly none of the suggested ways helped and I ended up nuking the WSL. Apparently, something more severe was broken.

Merissa answered 6/6, 2018 at 21:2 Comment(1)
I recommend reinstalling WSL from scratch, and never ever installing gcc from source unless you are willing to wipe your OS and reinstall!Polyp
J
14

Why compiling? You should be able to install the package:

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

Verify using:

gcc-7 --version

See How to install gcc-7 or clang 4.0?

Jacobi answered 6/6, 2018 at 21:5 Comment(5)
You are of course right, I should have mentioned that this was the first thing I tried but I ran into dependency hell. See updated question.Merissa
I have added another ppa. I have just verified on fresh Ubuntu 16.04 installed from Microsoft Store under Windows 10 x64 this works fine.Jacobi
What does the first command do? It didn't work in my case, but g++-7 --version reports it's installed, along with gcc-7 --version.Ovi
I think it adds another repository which can be used to get the installation files from. I do not understand apt much, though.Jacobi
That installed "gcc-7" for sure, but I really did need a "g++" binary. Afterwards I ran an apt install g++ and that just worked.Acicular
D
6

run the following

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

or

sudo apt install aptitude && sudo aptitude install golang gcc-7 g++-7
Dapple answered 8/6, 2018 at 5:38 Comment(0)
H
1

crt1.o is generally provided as part of the libdevc dependency (or something similar). I would suggest running sudo apt search libc or some similar variant with lib6c, libdev, libc-dev etc. Installing those fixed a similar issue I had recently.

Failing that, run find / -iname ctri.o and add the folder it appears in to your PATH with export PATH="[folder]:$PATH", and see if that helps.

Hystero answered 6/6, 2018 at 23:5 Comment(1)
No idea what crt1.o, libdevc, lib6c, libdev, or libc-dev are, nor what export PATH="[folder]:$PATH" is supposed to do, nor how these are related to installing g++. Gobbledygook to me.Ovi
A
0

As you can see here, g++ is just a package in the default ubuntu package repository (for ubuntu 18.04, 20.04 and 22.04 and others).

So it's actually enough to just run this.

sudo apt update
sudo apt install g++

Afterwards you can check with:

g++ --version

which resulted in version 9.4.0 at the time of writing.

However, you can also pick a specific version (e.g. apt install g++-12). However then the binary will also have a specific name (e.g. g++-12) and you may need to add a symlink if you need it to be g++. (e.g. ln -s /usr/bin/g++-12 /usr/bin/g++).

Acicular answered 9/11, 2022 at 19:54 Comment(1)
As you can see from my question, I actually tried this :) However, at the time of writing the question, there was no package for the specific version of GCC-7 as it was only released 5 months prior. The problem was that somewhere along the way the system just broke down.Merissa

© 2022 - 2024 — McMap. All rights reserved.