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.
$ sudo apt-get install clang-3.8
$ sudo apt-get install autoconf
$ sudo apt-get install make
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.
$ sudo update-alternatives --install /usr/bin/cc cc /usr/bin/clang-3.8 100
$ sudo update-alternatives --install /usr/bin/c++ cc++ /usr/bin/clang++-3.8 100
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.
$ sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-3.8 100
$ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-3.8 100
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.
$ sudo apt-get install git
$ git config --global core.autocrlf true
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.
$ sudo apt-get install vim
$ git config --global core.editor "vi"
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.
$ sudo apt-get install python-dev
$ sudo apt-get install python-pip
$ sudo pip install mercurial
We'll also set some useful settings in our ~/.hgrc file.
# Prevents warning : not trusting file .hg/hgrc from untrusted user root, group root
# If initial repository clone was done via windows the user and group will be set to root
# Stops us using vimdiff when resolving conflicts, people familiar with vi can omit this
# Lets us set the eol setting when cloning to windows where we will most often be editing the files
# We want files checked out with windows line endings