Use Make on Windows

Janne Kemppainen |

Make is an incredibly powerful tool for managing application compilation, testing and installation, or even setting up the development environment. It comes standard on Linux and macOS, and it is therefore widely adopted. But how can you get started with Make on Windows?

I’ve previously written about using Make for Python development, you might find it interesting.

If you are using Windows Subsystem for Linux (WSL/WSL2), then you can easily install make with the sudo apt install make command. However, if you want to have the command available natively on a normal terminal prompt then you need to install a Windows-specific version.

How to install Make on Windows?

The easiest way to configure Make is to use the Chocolatey package manager for Windows. It will handle downloading the correct executable, installing it, and making sure that the command is added to the system path.

  1. Install Chocolatey if you don’t already have it installed
  2. Open an elevated terminal (Run as administrator)
  3. Type the command choco install make, approve the installation if prompted

Next, verify that make works, and then you can start using it normally:

>> make --version
GNU Make 4.3
Built for Windows32
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Alternatively, if you’d prefer to use the new native Windows package manager you can use this winget command:

>> winget install GnuWin32.Make

This will download the installer and run it for you automatically. Make will also be added to the list of installed programs so you can uninstall it from the Add or remove programs section. You will still get the following error since the installer does not add make to the system path:

make : The term 'make' is not recognized as the name of a cmdlet, 
function, script file, or operable program...

To make it executable you need to open the Start menu and search for Edit the system environment variables. Click Environment Variables, then under System variables choose Path and click Edit. Click New and insert C:\Program Files (x86)\GnuWin32\bin, then save the changes. Open a new terminal and test that the command works.

As of writing this article the current version that gets installed with winget is 3.81, so it is older than the one from Chocolatey. You may want to take that into consideration when choosing the installation method. You can check which version would be installed with winget show GnuWin32.Make.

Using Make on Windows

From a syntax perspective there is no difference between Linux and Windows. You still need to write a Makefile and define the shell commands in tab-indented sections. However, the commands themselves need to be adjusted for the changed operating system.

Normally on a Makefile each line runs on a separate shell. If you need to run many commands from the same shell instance then they should be defined on the same line and chained together with the && operator.

.PHONY: test
test: venv
	.\venv\Scripts\activate && python -m unittest discover
	
.PHONY: venv
venv:
	python -m venv venv

The above example defines phony Make targets for setting up a Python virtual environment and running unit tests. Assuming that you have installed Python, running make test should execute successfully, running zero unit tests since it couldn’t find any.

If you need to make your Makefile support different operating systems, then you have to also detect the operating system to be able to run a different set of commands for each OS. With Windows the added difficulty is that in some cases (MSYS, MINGW) you actually need to use the Linux commands.

This answer on Stack Overflow has one solution for finding the correct environment, relying on how the system Path is delimited by semicolons ; unlike all other OSes. We can use that information to make our small example Makefile work natively on both Windows and Linux:

ACTIVATE := ./venv/bin/activate
PYTHON := python3
ifeq '$(findstring ;,$(PATH))' ';'
	ACTIVATE := .\venv\Scripts\activate
	PYTHON := python
endif

.PHONY: venv
venv:
	$(PYTHON) -m venv venv

.PHONY: test
os: venv
	$(ACTIVATE) && $(PYTHON) -m unittest discover

The command for activating the virtual environment is different on Windows compared to other OSes. Also the Python executable is named python on Windows, but on Linux you need to use python3. The conflicting commands can be defined as variables and then referenced in the targets.

Similarly, if you’re compiling C or C++ code, you need to use gcc or g++ on Linux, and cl on the Windows developer command prompt. Also the command arguments will need to be different.

Conclusion

It’s possible to use Make natively on Windows, and it’s not even that difficult. If you’re accustomed to using Bash and Linux commands then the switch to PowerShell commands might require a bit of adaptation. Definitely the biggest challenge is to write Makefiles that support different operating systems, but as we saw that can be accomplished with some tricks.

Subscribe to my newsletter

What’s new with PäksTech? Subscribe to receive occasional emails where I will sum up stuff that has happened at the blog and what may be coming next.

powered by TinyLetter | Privacy Policy