Как запустить Make на Windows
Make — это широко используемая для автоматизации сборки проектов утилита, которую бывает проблематично установить и запустить на windows. Сегодня я поделюсь самым простым способом, который позволит вам это сделать. Использовать мы будем Chocolatey.
Chocolatey (choco) — это менеджер пакетов для Windows, который позволяет устанавливать и управлять программным обеспечением из командной строки. Вот как установить утилиту make
на Windows с помощью Chocolatey:
-
Установка Chocolatey:
-
Откройте PowerShell от имени администратора.
-
Вставьте следующую команду и нажмите Enter:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Эта команда скачает и запустит скрипт установки Chocolatey. Подтвердите выполнение команды, если будет запрошено разрешение.
-
Установка make с помощью Chocolatey:
После установки Chocolatey выполните следующую команду в PowerShell:
choco install make
Chocolatey автоматически загрузит и установит утилиту make
и все необходимые зависимости.
-
Проверка установки:
make --version
Если установка прошла успешно, вы увидите вывод с информацией о версии make
.
Далее, для использования make можно использовать обычный CMD, но, всегда в режиме администратора.
Если было полезно — подписывайтесь и ставьте лайки. Благодарю за внимание.
Make in Windows
Description of how to install and use Make in Windows OS.
There are several ways to install Make on Windows. In this tutorial we will use Git Bash because it is also needed for on Windows, so you might already have that if you followed the steps in . The steps follow the instructions detailed . After installing Git & Git Bash:
-
Download
make-4.4-without-guile-w32-bin.zip
(get the version without guile). -
Copy the contents to your
C:\Program Files\Git\mingw64\
merging the folders, but do NOT overwrite/replace any existing files.
Using Makefiles in different environments
Commands are called differently in different environments, for example, if you want to run Stata in Git Bash terminal on Windows you should use StataMP-64
, but stata
on Mac and Linux. Aliases don’t work well in our setting (as Make is run in Git Bash, but Make itself uses the sh shell). Although if you include an operation system detector part at the beginning of your Makefile, it provides a simple solution for a reproducible Makefile in different environments.
Let’s create a project folder called trial/
, where the codes can be run with a Makefile both on Windows and Mac or Linux. There should be 2 files in the folder: trial.do
and Makefile
. The trial.do
creates a trial.log
just to see and check whether Make runs correctly. The content of trail.do
is the following:
capture log close
log using "trial.log", replace text
disp "DateTime: $S_DATE $S_TIME"
log close
You can copy the following content in your Makefile
:
#OS detector part
ifeq ($(OS),Windows_NT) # is Windows_NT on XP, 2000, 7, Vista, 10... STATA := StataMP-64 else STATA := stata endif
#Code starts here
trial.log: trial.do $(STATA) -e do $<
When you finished, open the Git Bash terminal, go to the trial/
folder where the trail.do
and your Makefile
is, and then run make
.
$ cd ~/.../trial/
$ ls
trial.do
Makefile
$ make
Afterward, you should see the trial.log
created by the Makefile
.
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Sign up
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.
- Install Chocolatey if you don’t already have it installed
- Open an elevated terminal (Run as administrator)
- 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.
Why Build Tools?
Imagine you have built a complex project, consisting of dozens of datafiles and scripts.
Also imagine you haven’t worked on the project for a few weeks, and now wish to continue
your work where you left off.
The big questions then become:
- In which order do the files need to be executed?
- Which files are up-to-date and do not have to be executed again?
For this purpose, we use a workflow management system — or, in technical terms — «build tool».
Build tools will allow us to control the execution of a set scripts by running them from the command line.
Some reasons we push this topic are:
- Your workflow / order of execution is explicitly documented.
- Each time you run
make
, it only executes each script if the output is expected to be different from the last time your ran it. That is, it runs ‘partial builds.’ - If multiple users work on the project, they can easily execute code that others have written.