Installing G++ on Windows Subsystem for Linux

Installing g++ on windows subsystem for linux

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?

How to install GCC and GDB for WSL(windows subsytem for linux)?

You need to:

  1. Update and upgrade:
$ sudo apt-get update && sudo apt-get upgrade -y

Sample Image

2.Clean unrequired packages:

$ sudo apt autoremove -y

Sample Image


  1. Install GCC:
$ sudo apt-get install gcc -y

Sample Image


  1. Check and confirmed installed gcc version:
gcc --version

Sample Image

Is it possible to compile from Windows to Linux with gcc/g++?

You have several choices:

  1. WSL.
    WSL(Windows Subsystem for Linux) its linux termanal in windows, so you can compile linux code in windows. This solution is the simpliest and I would recommend to use it.
  2. Visual studio.
    Visual studio has a package that allows you to compile programs for Linux. More details here

Newly installed g++ on wsl has problem recognizing c++11 STL types

The std::shared_mutex was not provided until c++ 17. https://en.cppreference.com/w/cpp/thread/shared_mutex

So to fix this issue, you just need to change the compilation command to

g++ rwlock.cpp -std=c++17

How to compile executable for Windows with GCC with Linux Subsystem?

Linux Subsystem works as a Linux-computer. You can only run Linux executables inside it and default gcc creates Linux executables.

To create Windows executables, you need to install mingw cross-compiler:

sudo apt-get install mingw-w64

Then you can create 32-bit Windows executable with:

i686-w64-mingw32-gcc -o main32.exe main.c

And 64-bit Windows executable with:

x86_64-w64-mingw32-gcc -o main64.exe main.c

Note that these Windows executables will not work inside Linux Subsystem, only outside of it.



Related Topics



Leave a reply



Submit