How to run makefile on windows

This article explores alternatives to Makefiles on Windows. Earthly provides a consistent, containerized build environment for Windows users. Check it out.

As the field of DevOps and build release engineering continues to grow, many new tools are being developed to help make building and releasing applications easier. One of the tools that has been in use for many years is Make, which is still heavily used by engineers today.

A Makefile is a simple text file consisting of targets, which can invoke different actions depending on what has been configured. For example, with a Makefile, you can invoke a build of your application, deploy it, or run automated tests and it can dramatically increase the efficiency of your workflow.

Initially, it was Stuart Feldman who began working on the Make utility back in 1976 at Bell Labs. However, the version of Make most commonly used today is GNU Make, which was introduced in the late 1980s.

While the tool was originally meant to run on Linux, Make’s popularity has interested those working on other operating systems as well. There are several ways to run Makefiles on Windows, and in this article you’ll be introduced to each option and learn about their strengths and weaknesses.

Using Make on Windows

windows

Before looking at the different options available, you should know why you want to run Makefiles on Windows in the first place. Or rather, if you’re working on Windows, why are you even interested in Makefiles?

Historically, the biggest reason for wanting Makefiles to run on Windows is that the developers in your organization are working on Windows. Seeing as how the de facto standard for languages like C and C++ is to use Make, it’s no wonder that Windows users want the ability to use Make as well.

As applications and infrastructure become more modern, the cloud is another reason for wanting Makefiles on Windows. Many infrastructure engineers want their applications to be run on Linux, likely led by the adoption of tools like Docker and containerization in general. Additionally, on Linux, a Makefile is the primary tool to use in many cases, especially when it comes to building native Linux applications. However, many engineers are still using Windows on their workstations, leading to the question of how to run Makefiles on Windows. Let’s dive into the possible answers.

Chocolatey

chocolatey

Linux users have been using package managers for decades, yet they’ve never gained much traction on Windows. Up until the release of winget, the concept of a package manager was never something that was natively included on Windows. Instead, Rob Reynolds started working on an independent package manager back in 2011 that would come to be known as Chocolatey. Chocolatey is now widely used on Windows to install packages, and you can use it to install make as well.

To do so, run the following command in an Administrative PowerShell window:

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'))

You can find the newest installation instructions at any time on the Chocolatey website.

Once Chocolatey is installed, you may have to close down the PowerShell window and open it back up. After that, run the following command:

Once the script is done running, make will be installed. You may need to restart the PowerShell window again, but at this point you are ready to use Makefiles on Windows.

Chocolatey will likely be the most popular option for those who want to stick to a pure Windows installation. It’s easy to install, easy to use, and you don’t need to jump through any hoops or workarounds to get it working.

At this point, you can use make just like you otherwise would, and you can test it by running make -v.

Cygwin

Historically, one of the most popular ways of running any type of Linux functionality on Windows has been to use Cygwin. Cygwin aims to give a Linux feeling to Windows by holding a large collection of GNU and open source tools. It’s important to note that this does not mean it will give you native Linux functionality. However, it does allow you to use Linux tools on Windows. There’s a big difference between the two; for instance, Cygwin does not have access to Unix functionality like signals, PTYs, and so on. It’s a great tool for when you want to use familiar Linux commands but still want them to be run on Windows.

To use Cygwin for Makefiles, start by downloading and installing Cygwin. During the installation, you’ll see a window popping up asking you what packages you want to install. In the top left corner, make sure to select Full and then search for make.

Searching for “make”

Your search will give you a list of several different packages. You want to choose the one that’s labeled just as make. Change the dropdown menu where it says Skip to the latest version.

Choosing “make”

Now you can finish the installation by clicking Next in the bottom right corner. Once the installation is done, you can open up Cygwin and verify that make has been installed by executing make --version.

NMAKE

One of the alternatives that you’ll often hear about regarding running Makefiles on Windows is NMAKE. While it is an alternative to make, note that you cannot simply take your existing Makefiles from Linux and run them using NMAKE; they have to be ported.

First of all, the compilers are different on Windows and Linux, so if you are specifying your compiler in your Makefile, you’ll have to change that to whatever is relevant on Windows. At the same time, you’ll have to change the flags that you send to the compiler, because Windows typically denotes the flags using / instead of -.

On top of that, it doesn’t recognize all the syntax that you’re used to from GNU Make, like .PHONY. Lastly, Windows obviously doesn’t recognize the commands that work on Linux, so if you have specified any Linux-specific commands in your Makefiles, you’ll also have to port them.

All in all, if your entire organization uses Windows and you simply want the typical functionality of GNU Make, then NMAKE is a viable solution. However, if you just want to quickly run your traditional Makefiles on Windows, NMAKE is not the answer.

CMake

cmake

As with NMAKE, CMake is not a direct way to run your Makefiles on Windows. Instead, CMake is a tool to generate Makefiles, at least on Linux. It works by defining a CMakeLists.txt file in the root directory of your application. Once you execute cmake, it generates the files you need to build your application, no matter what operating system you’re on.

On Linux, this means that it creates Makefiles for you to run, but on Windows it may mean that it creates a Visual Studio solution.

CMake is a great solution if you don’t care too much about running Makefiles specifically, but you want the functionality, namely the ease of use in a build process, that you can get from Makefiles.

Windows Subsystem for Linux

The Windows Subsystem for Linux (WSL) is an honorable mention. It’s cheating a bit to say that it’s a way to run Makefiles “on Windows,” as your Makefiles won’t actually be running on Windows.

If you haven’t heard of WSL before, here’s an extremely oversimplified explanation: It uses Hyper-V to create a hyper-optimized virtual machine on your computer, in which it runs Linux. Basically, you get a native Linux kernel running on your Windows computer, with a terminal that feels as if it’s part of Windows.

You should look into WSL if what you care about most is having Windows as your regular desktop environment, but you’re fine with all of your programming and development going on inside of Linux.

Conclusion

As you can see, there are a few different ways you can be successful in running Makefiles on Windows. However, you do need to be wary of the fact that it will never be a perfect solution. Every solution is in some way a workaround, and the closest you’ll get to feeling like you’re using native Makefiles while using Windows is to install something like WSL.

If you enjoyed this tutorial you might want to grab a copy the free eBook “Makefile ins and outs”. In it you’ll learn many tips and tricks for structuring builds and scaling up a build process. From how to write a simple Makefile to how to deal with dependencies and everything in between. Download this free Makefile book right now.

A Makefile is a special file that contains information about a program’s flow, targets, and libraries. To use it, we need to access the Make command. However, it is not preinstalled on Windows. In this post, we will learn how to install and run Makefile on Windows.

A Makefile is a file that contains information about a program’s flow, targets, and libraries. It is also known as a description file as it includes a group of targets and the sources on which they depend. Additionally, the Makefile contains libraries that support the source code. This type of file is executed using the make command. To install and run Makefile on your computer, you are supposed to follow the steps mentioned below.

  1. Download and install MinGW
  2. Configure Environmental Variable
  3. Install the Make command
  4. Rename mingw32-make.exe to Make
  5. Create and run the Makefile

Let us talk about them in detail.

1] Download and install MinGW

MinGW is the Windows adaptation of the GNU Compiler Collection (GCC), offering freely distributable import libraries and header files essential for developing native Windows applications. To use Make, we first need to download the MinGW installation media. Once downloaded, run the installation media, make sure to untick“also install support for the graphical user interface.”, and follow the on-screen instructions to complete the process. It will take some time to complete the installation process, so wait, and once done, click on Continue or Quit.

2] Configure Environmental Variable

After installing MinGW, we need to add the environmental variable to access the ‘Make’ command. For this, we need to first fetch the path of MinGW and add it to the environmental variables. Follow the steps mentioned below to copy the path of the mingw-get file.

  • Open File Explorer by Win + I.
  • Go to the location where you have installed MinGW. If you didn’t make any changes to the default location, go to C:\MinGW.
  • Double-click on Bin to open the folder.
  • You need to copy the path of the folder, as that would be the path of mingw-get.

Once you know the path of the file, follow the steps mentioned below to add it to the Environmental Variable.

  1. Open Control Panel.
  2. Search for “Environmental Variables”.
  3. Click on Edit the system environmental variable.
  4. Now, click on the Environmental Variable button.
  5. From the System variables section, click on Path, and then on Edit.
  6. Click on New and then paste the path we asked you to copy earlier.
  7. Click on Ok.

Now you can close all interfaces as we have added the environmental variable.

3] Install the Make command

After adding the environmental variable, we can install the Make command. However, you won’t be able to run the Make command without following the previous steps. To do so, open the Command Prompt as an administrator and then run the following command.

mingw-get install mingw32-make

When you run this command, MinGW will install all the modules related to Make; however, it will take a while to do so.

To know if it is installed, you can run mingw32-make --versioncommand. It will give you the version information about the MinGW installation.

4] Rename mingw32-make.exe to Make

install and run Makefile

Don’t you think, running ‘mingw32-make’ every time to use the ‘make’ command is a bit of a hassle? If yes, we would rename mingw32-make to Make so that next time we can just use the ‘make’ command. To do so, follow the steps mentioned below.

  1. Open File Explorer.
  2. Go to the location where you have installed mingw32.
  3. Now, double-click on Bin, and then rename mingw32-make file to make.

To verify this, run make --versionIf the command is running without throwing any error, you can rest assured that renaming worked.

5] Create and run the Makefile

Upon installing the Make command, we can create a Makefile. There are various ways to do this, but we have listed a general guide below that you can follow.

  • You can create a new folder with a name of your choice or the Makefile on your desktop; that’s what we did.
  • Now, create a text file. To do so, right-click on a space and select New > Text document.
  • Open the file and paste the following lines of code. Make that before starting the second line, you give a Tab.
firstmakefile:
      echo "Getting started with Makefile";
  • To save it, go to File > Save as. 
  • Give it a name, let’s say, Makefile, change the Save as type to All files, and click on Ok.
  • To run the file, open Command Prompt.
  • Navigate to the location where you have created the folder, for that, run the cd command. Since my location is C:\Users\yusuf\OneDrive\Desktop, I ran cd C:\Users\yusuf\OneDrive\Desktop.
  • Run dir to list all the files present in that particular directory.
  • To run the file, just run make -f filename.

That’s how you can create and run a Makefile.

Read: How to install NumPy using PIP on Windows

What is the command to run a makefile?

To run a makefile, you are required to use the make command. However, if you want to call a file, use -f option so that the command would look like make -f filename.

Read: How to create a file without Extension in Windows

How do I install Make on Windows?

To install Make on your computer, we can install MinGW first, then add the environmental variable path, and finally, install Make. We recommend that you follow the guide mentioned earlier to install Make on your system.

Read: 5 System Information Tools in Windows.

For software developers working on Windows, the process of running a Makefile can sometimes be a bit tricky. Makefiles are commonly used as build automation tools in software development projects, allowing developers to specify the dependencies and build targets for their code.

Running Makefile on Windows Using Cygwin

However, Makefiles are traditionally used in Unix-based operating systems, and running them on Windows can be challenging.

One popular solution for running Makefile on Windows is using Cygwin, a Unix-like environment and command-line interface for Microsoft Windows. Cygwin provides a collection of tools and libraries that allow you to run Unix applications on Windows, including the GNU Make utility which is used to interpret Makefiles.

What is Cygwin?

Cygwin is a free and open-source software that provides a Unix-like environment on Windows. It consists of a library that emulates the POSIX system calls and a collection of tools that provide a Unix-like command-line interface. With Cygwin, you can run many Unix utilities and applications on Windows, including the GNU Make utility which is essential for running Makefiles.

One of the key features of Cygwin is its ability to provide compatibility between Unix-based systems and Windows. This compatibility allows developers to run Makefile-based build processes on Windows without having to rewrite the build scripts for the Windows environment.

Installing Cygwin

To run Makefile on Windows using Cygwin, you first need to install Cygwin on your Windows system. The installation process is straightforward and involves downloading the Cygwin installer from the official website and running it on your system.

  • Download the Cygwin installer from the official website.
  • Run the installer and select the packages you want to install. Make sure to select the GNU Make package.
  • Follow the on-screen instructions to complete the installation process.

Once you have installed Cygwin on your Windows system, you can open the Cygwin Terminal and navigate to the directory containing your Makefile to run it using the GNU Make utility.

Running Makefile with Cygwin

To run a Makefile on Windows using Cygwin, you first need to open the Cygwin Terminal and navigate to the directory containing your Makefile. Once you’re in the right directory, you can use the make command to run the Makefile and build your project.

For example, if your Makefile is named Makefile, you can run it by typing the following command in the Cygwin Terminal:

make

After running the make command, Cygwin will interpret the Makefile and execute the build process according to the rules specified in the Makefile. You can also pass additional parameters to the make command to specify the build target or any custom options defined in the Makefile.

Benefits of Using Cygwin for Running Makefile on Windows

Using Cygwin to run Makefile on Windows offers several benefits for software developers:

  • Compatibility: Cygwin provides compatibility between Unix-based systems and Windows, allowing developers to run Makefile-based build processes on Windows without any modifications to the build scripts.
  • Productivity: By using Cygwin, developers can leverage the powerful GNU Make utility and other Unix tools to streamline the build process and increase productivity.
  • Flexibility: Cygwin allows developers to run a wide range of Unix utilities and applications on Windows, providing flexibility in development environments and toolsets.

In conclusion, Cygwin is a powerful tool for software developers working on Windows who need to run Makefile-based build processes. By installing Cygwin and leveraging its compatibility with Unix-based systems, developers can streamline their build processes and increase productivity. If you’re looking to run a Makefile on Windows, give Cygwin a try and see how it can improve your development workflow.

Running a Makefile on Windows Using MinGW

In this blog post, we will guide you through the process of running a makefile on Windows using MinGW.

What is MinGW?

MinGW, short for Minimalist GNU for Windows, is a development environment for Windows that allows you to use GNU tools such as GCC and Make to build and run software. It provides a Unix-like environment on Windows, making it easier for developers to work on cross-platform projects.

Setting Up MinGW

The first step in running a makefile on Windows is to install MinGW on your system. You can download the MinGW installer from the official website and follow the installation instructions to set it up on your Windows machine.

Once MinGW is installed, you will need to add the MinGW bin directory to your system Path. This allows you to run MinGW commands from any directory in the command prompt. To do this, go to Control Panel > System and Security > System > Advanced system settings > Environment Variables, and edit the Path variable to include the MinGW bin directory.

Creating a Makefile

Now that you have MinGW set up on your system, you can create a makefile for your project. A makefile is a text file that contains instructions for building your software project, such as compiling source code files and linking them together.

Here is an example of a simple makefile:

  • CC=gcc
  • CFLAGS=-Wall
  • all: hello.exe
  • hello.exe: hello.c
  • $(CC) $(CFLAGS) -o hello.exe hello.c

  • clean:
  • rm -f hello.exe

In this makefile, the CC variable is set to the GCC compiler, and the CFLAGS variable is set to enable all compiler warnings. The all target builds the hello.exe executable from the hello.c source file, and the clean target removes the executable file.

Running the Makefile

To run the makefile on Windows using MinGW, open a command prompt and navigate to the directory where your makefile is located. Run the following command to build the project:

  • make

This command instructs the Make tool to read the makefile and build the project according to the instructions provided. If there are no errors in the build process, you will see the compiled executable file in the same directory.

Benefits of Using MinGW

Using MinGW to run makefiles on Windows offers several benefits to developers:

  • Compatibility: MinGW provides a Unix-like environment on Windows, allowing developers to use familiar tools and workflows.
  • Flexibility: MinGW supports a wide range of GNU tools, making it easy to build and run software projects on Windows.
  • Performance: MinGW delivers high performance and optimization for compiled code, ensuring that your applications run smoothly on Windows.
  • Community Support: MinGW has a large and active community of developers who can provide help and support for using MinGW on Windows.

Running a makefile on Windows using MinGW can help streamline your software development process and improve your productivity as a developer. By following the steps outlined in this blog post and leveraging the power of MinGW, you can easily build and run software projects on Windows with ease.

Have you tried running makefiles on Windows using MinGW? Share your experience with us in the comments below!

Running a Makefile on Windows Using WSL

This is the part where we explore how you can utilize WSL to run a Makefile on Windows.

What is WSL?

WSL is a compatibility layer for running Linux binary executables natively on Windows. It allows developers to use Linux tools and utilities on a Windows machine, providing a seamless experience for cross-platform development. With WSL, you can access the Linux shell and run commands as if you were working on a Linux system.

Setting Up WSL

To run a Makefile on Windows using WSL, you first need to install WSL on your Windows machine. You can enable WSL by following these steps:

  • Open Control Panel and navigate to Programs and Features
  • Click on «Turn Windows features on or off»
  • Check the box next to «Windows Subsystem for Linux» and click OK
  • Restart your computer to apply the changes

Installing a Linux Distribution

Once you have enabled WSL, you can install a Linux distribution of your choice from the Microsoft Store. Popular options include Ubuntu, Debian, and Kali Linux. After installing a Linux distribution, you can launch it from the Start menu and set up your username and password.

Running Makefile on Windows

With WSL set up, you can now navigate to the directory containing your Makefile using the Linux shell. You can use the `cd` command to change directories and the `ls` command to list the contents of a directory.

Once you are in the directory with your Makefile, you can run the `make` command to execute the Makefile. The Makefile will then compile your code according to the rules specified in the Makefile. If there are any errors during the build process, the Makefile will display them in the terminal.

Benefits of Using WSL for Makefiles

Using WSL to run Makefiles on Windows offers several advantages for developers:

  • Cross-platform Development: WSL allows you to leverage Linux tools and utilities on a Windows machine, enabling seamless cross-platform development.
  • Easy Integration: With WSL, you can easily integrate Linux build processes into your Windows workflow without the need for a separate Linux machine.
  • Compatibility: WSL ensures compatibility with Linux-based build systems, making it easier to work on projects that require Linux-specific tools.

Running a Makefile on Windows using WSL provides developers with a convenient way to automate the build process of their projects. By leveraging the power of WSL, you can seamlessly run Linux-based tools and utilities on a Windows machine, streamlining your development workflow and enhancing productivity. Try out WSL today and experience the benefits of cross-platform development on Windows!

DOCKER

If you’ve dabbled in programming, especially if you come from a Unix or Linux background, you’re probably familiar with the convenience of Makefiles. For those new to the concept, a Makefile is a special file that describes how to build and compile a program. It dictates what commands need to be run to generate the executable from the code. But what if you’re working on a Windows system? Fear not, dear reader, as we dive into the ins and outs of running a Makefile in a Windows environment.

Understanding Makefiles

Before we jump into running them on Windows, let’s have a quick primer on what Makefiles are. A Makefile is a plaintext file that contains a set of directives used by a make build automation tool to compile and build programs. Essentially, it automates the compilation process, reducing the complexity of firing a series of commands down to just one or a few simple commands.

Commonly, a Makefile includes the following components:

  • Targets: These are the specific tasks or goals defined in the Makefile, such as build, clean or install.

  • Prerequisites: Files that need to be present before a target can be executed, such as source files.

  • Recipes: The commands that make will execute.

For instance, here’s a very simple example of a Makefile:

all: hello

hello: hello.c
    gcc -o hello hello.c

clean:
    rm -f hello

This Makefile has two targets: all (which is often the default goal for a Makefile) and clean. The target hello has a dependency on hello.c file, and the recipe states that gcc (GNU Compiler Collection) should be used to compile the hello.c file into an executable named hello.

Now, let’s get them running on Windows.

Running Makefiles in Windows

1. Using a Compatibility Layer: Cygwin or WSL

Windows lacks the native ability to run Unix-like build scripts out of the box, but that doesn’t mean you’re out of luck. You can use a compatibility layer like Cygwin or the Windows Subsystem for Linux (WSL).

Cygwin is a POSIX compatible environment that runs natively on Windows. After installing it, you can use its terminal just like you would in Unix/Linux to run Makefiles.

WSL, on the other hand, lets you run a Linux distribution alongside your Windows installation. The Windows 10 Spring 2018 update and later versions come with this feature that you can enable and install a Linux distribution of your choice through the Microsoft Store.

Here’s how to use WSL for running Makefiles:

# First, make sure that WSL is enabled and your Linux distro is installed.

# Open your WSL terminal and navigate to your source code directory using `cd`.

cd /mnt/c/path/to/source/code

# Once there, you can use the `make` command as you would in a Linux environment.

make

2. Using MinGW / MSYS

Alternatively, Windows users can install MinGW (Minimalist GNU for Windows), which provides a complete Open Source programming toolset. Along with MinGW, you want to install MSYS, a collection of GNU utilities such as bash, make, gawk, and grep to allow building applications.

Once installed, you can open the MinGW shell and navigate to your project directory where the Makefile resides:

# Run make to compile your code according to the Makefile.
make

3. Installing Make with Chocolatey

For those who wish for a more straightforward method, you can install ‘make’ using a package manager for Windows such as Chocolatey.

First, install Chocolatey following the instructions on their website. Then, run the following command in an administrative command prompt or PowerShell to install make:

Now you can run make from the regular command prompt or PowerShell:

cd path\to\source\code
make

Troubleshooting Common Issues

When running Makefiles in Windows, some common problems can occur:

  • Line Endings: Make sure the Makefile uses Unix-style LF line endings, not Windows-style CRLF.

  • Pathnames: Windows’ pathnames are different from Unix/Linux, so update the Makefile to reflect the correct paths.

  • Command Differences: Some commands in Unix Makefiles may not work in Windows or have different names.

Conclusion

Running Makefiles in Windows isn’t as straightforward as on Unix-like systems, but by leveraging compatibility layers or installing necessary packages through MinGW or Chocolatey, Windows users can enjoy the utility of Makefiles. Following the steps outlined above should provide a smooth experience when dealing with Makefiles, significantly simplifying your C or C++ development process on a Windows system. Happy coding and building!

Remember to test your setup and possibly adapt your Makefiles to accommodate the Windows environment. With a bit of tweaking, the power of Makefiles can be fully harnessed on any platform.

Suggested Articles

DOCKER

DOCKER

DOCKER

DOCKER


Best Practices To Optimize Docker Image Size

Docker has revolutionized how we develop, deploy, and run applications by allowing us to encapsulate our environments into code. The containerization platform makes it possible to run our apps anywhere, anytime, with the guarantee that they’ll work j…

DOCKER

DOCKER

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.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Aero lite для windows 10
  • Электронная активация windows 10
  • Подключение блокируется правилом брандмауэра защитника windows
  • Можно ли установить программы для windows на linux
  • Как создать логический диск в windows 11