DEV Community

Martin Licht
Martin Licht

Posted on

Using Clang++ and Make natively on Windows

This guide will explain you how to install Clang++ and Make on Windows:
specifically, we will discuss the MinGW-w64 project.

In my experience, getting these programs to run natively on Windows is faster than calling them from within emulation layers such as Cygwin.

Table of Contents

Downloads

  1. Open your web browser and load the official project website of the MinGW-w64 project:

    https://www.mingw-w64.org/

    In fact, we can go straight to the download section:

    https://www.mingw-w64.org/downloads

  2. You find several links to different packages of MinGW-w64 bundled together with compilers and additional software.

    This guide focuses on the LLVM package, LLVM-MinGW, which provides the LLVM compilers and also comes bundled with make and Python (Version >= 3.9).

  3. Use the link on the website or go directly to:

    https://github.com/mstorsjo/llvm-mingw/releases

    Here, you find Zip-files that contain the precompiled binaries, for different processor architectures and operating systems.

  4. We need to understand which version we want to download. A brief explanation is found on the project homepage on Github:

    LLVM MinGW Releases

    We want to work on Windows directly, so we look for Zip-files called

    llvm-mingw-<version>-<crt>-<arch>.zip
    

    where

    1. <version> - A running number that simply indicates the version of the package
    2. <crt> - The C runtime library required, which is either mscvrt or ucrt. The former is the MSV C runtime, which is available on all versions of Windows but has less features. The latter is the Universal C runtime, which is pre-installed from Windows 10 onwards. Please note that the Zip-file will not provide that runtime library, you need to make sure that your Windows installation already provides it. For that reason, we will pick the MSV C runtime.
    3. <arch> - This indicates the processor architecture, which is one of the following: - i686, which is the 32-bit architecture of Intel - x86-64, which is the 64-bit architecture of Intel - armv7, which is the 32-bit architecture of ARM - aarch64, which is the 64-bit architecture of ARM All the possible packages contain binaries to compile for any of those architectures but these binaries only run on the specified architecture. For that reason, you need look up which processor is being used on your machine and pick the appropriate package.

    Let us suppose from now on that we have picked 64-bit Intel CPU and, just for compatibility reasons, use the MSV C runtime.
    The version will be 20231003.
    You must adapt the following lines according to your choice of package, and likely your version number will be more advanced.

    Whatever package you choose, download the Zip file.

Installing and testing clang and make

  1. The installation begins with simply unpacking the .zip archive and transferring the folder to a convenient location. For simplicity, we will put our copy in the top folder of the main drive C:\. All the binaries are now contained in:
   C:\llvm-mingw-20231003-msvcrt-x86_64
Enter fullscreen mode Exit fullscreen mode
  1. The most important subfolder is:
   C:\llvm-mingw-20231003-msvcrt-x86_64\bin
Enter fullscreen mode Exit fullscreen mode

which contains the binaries that we want to invoke.

  1. We modify the Windows path variable. For that, we need to access the Advanced System Settings.

    • Open the launcher and enter Advanced System Settings.
    • Alternatively, open Control Panel, then System and Security, then System, then look for Advanced System Settings.
  2. Once the Advanced System Settings dialog is open, titled System Properties, open the tab Advanced and click on Environment Variables....

  3. In the first list, click on Path and then Edit....

    • Click on New and enter the path of the binary folder. In our example:
     C:\llvm-mingw-20231003-msvcrt-x86_64\bin
    
  • Then click OK on all remaining open dialogues.
  1. The installation should be ready to use by now.
    We can try either Git Bash, Cygwin, Windows PowerShell, or the Methuselian cmd.exe.
    All of these load the Windows path variable.

    1. Open any of those command prompts and enter:
      clang --version
    

    That should display some self-description of Clang, including the version number.

    1. Make has a different name in this package. Open any of the aforementioned command prompts and enter:
      mingw32-make --version
    

    Again, you will receive the version output of your current make implementation.

    1. You can look up any of the other commands in the binary folder if you need something else.

One word of caution: different command prompts will try to look up the corresponding binaries in various places, prioritizing one folder over another.

  • For example, when calling gcc, Cygwin will first look in its own internal directories for gcc before any MinGW32 implementation. So if your Cygwin installation already includes GCC, then Cygwin will pick that one.
  • Another example: calling make in Cygwin will call the binary at /usr/bin/make, but mingw32-make will be found in your Windows system.

Unix utilities on Windows PowerShell

  1. We focus now on Makefiles and their use via Windows PowerShell:
    if your makefiles have been developed for a Unix environment, then you may encounter difficulties when executing make on Windows.
    Things will typically work fine when the shell is, say, Git Bash, which provides the most basic Unix utilities (such as touch, cp, and so on).

  2. There are circumstances, however, where you also want to execute our makefile in Windows PowerShell.
    For example, some IDEs for Windows use PowerShell as their standard terminal.
    But not all basic Unix utilities are available in their full capacity on Windows (e.g., cp -p is missing).
    We can make the standard Unix tools available on PowerShell as follows, assuming that Git Bash has already been installed.

  3. First, check the location of the binary files that come with Git Bash. For instance, that folder might be:

   C:\Program Files\Git\usr\bin\
Enter fullscreen mode Exit fullscreen mode

or whatever else is your installation folder for Git Bash.

  1. We once again modify the Windows path variable.
    We open the launcher and enter Advanced System Settings.
    Alternatively, we open Control Panel, then System and Security, then System, then look for Advanced System Settings.

  2. Once that Windows is open, named System Properties, open the tab Advanced and click on Environment Variables....

  3. In the first list, click on Path and then Edit....
    Click on New and enter the path of the binary folder for Git Bash,
    as identified above. Then click OK on all open dialogues.

  4. Now the Unix commands that come with Git Bash should also be available in Windows PowerShell.
    To test that, open a Windows PowerShell terminal, and type something like:

   touch --version
Enter fullscreen mode Exit fullscreen mode

You should now see what you would expect on Linux as well.
Similarly, the same should now work on the classical Windows command line terminal.

Top comments (0)