This post covers the starting steps required to start developing c or c++ programs for Linux inside Windows 10 using Bash on Ubuntu on Windows.
Since the Windows 10 Anniversary Update (build 10.0.14393, version 1607) there is an Ubuntu bash shell available from within Windows without the need for a virtual machine or dual booting. Unlike Cygwin or MinGW it runs real Linux binaries directly from within Windows. This allows us to edit, compile and run Linux applications from Windows without the need for switching computers or operating systems.
Enabling Linux shell in Windows 10
The computer will reboot several times during this process.
- First we need to have the Windows 10 Anniversary Update installed, for more info on how to install that see here.
- Next navigate to the developer settings page by pressing the windows key and typing “For developers settings”. If that doesn’t work click the start button, the Settings cog icon, select the Update & security page and then the For developers tab.
- Enable Developer mode.
- Next find the Turn Windows Features On or Off window by pressing the windows key and typing the name of the window. Alternatively navigate to Control Panel, Programs and Features and find it there.
- Enable the Windows Subsystem for Linux (Beta) feature in the list and click OK.
- Once the computer has restarted launch the newly installed bash terminal. The first time this is run it will download the bash package from the Windows Store.
- After the bash package has downloaded we are asked to enter a new username and password. This password will be required when using sudo later so make sure it is something memorable.
Installing the compiler and tool chain
The fresh install of Ubuntu bundled with the Windows 10 Anniversary Update is very bare bones. To do any development we need a compiler and some configuration tools.
clang was chosen over gcc as it has better support for c++11, c++14 and c++17 than the latest available version of gcc (version 4.8).
Make the newly installed clang-3.8 the default c and c++ compiler.
Most packages will look for cc or c++ when trying to compile, as we don’t have those we have to register clang as our alternative c and c++ compilers.
We’ll also need to point the default version of clang to our version clang-3.8 so attempts to call clang or clang++ find the installed version.
As well as installing the git package we will also set the line endings to be checked out as crlf, Windows style, but committed as lf, Linux and Mac style, as we will primarily be working with the sources using Windows text editors.
By default git seems to use nano as its editor, this can also be optionally changed. At the moment there is no way of invoking a Windows GUI editor from within linux so we’ll just use vim as an example.
The apt-get package for mercurial has dependencies on a lot of tcl and x packages so instead of installing that we will use the python version.
We’ll also set some useful settings in our ~/.hgrc file.