This post is to help my CSE115 classmates at North South University. I’ll try my best to explain you how to compile C program using Makefile feature on windows platform. I’ll be discussing two things, how to install C compiler and how to write Makefile and compile using it.
You may have been using Codeblocks or some other IDE to code in C language. That IDE have a compiler installed inside it. Now you have to make the compiler usable with windows Command Prompt.
First thing you will do is check if it is already installed in Command Prompt. Open Command Prompt by opening start menu or start screen and type ‘cmd’ then pressing enter. In Command Prompt type ‘gcc –version’ or ‘g++ –version’. If you get output something like ‘gcc is not recognized’ or ‘g++ is not recognized’ then the compiler is not installed in your system properly. And if you get output something like ‘gcc (tdm-1) 4.9.2’ then the compiler is installed, you can skip to “Using Makefile“.
Now you have to find the compile inside your IDE – Codeblocks. Go to this path “C:\Program Files (x86)\CodeBlocks” or this path “C:\Program Files\CodeBlocks” and see if you can find a folder named “MinGW”. If you can’t find that inside your IDE’s folder, try using windows search option to find it or download it from here: https://sourceforge.net/projects/mingw/files/.
After that go to ‘Mingw’ folder and then to the ‘bin’ folder. Copy the path of the ‘bin’ folder, in my case it is “C:\Program Files (x86)\CodeBlocks\MinGW\bin”. Now we have to set this path to windows’ environment variable. Open “This PC”, right click on any blank area and select properties from the menu. Then go to “Advance system settings” and then click on “Environment Variables”. Now find the variable “path” from there. Select that variable and click ‘Edit’. Now please be careful, don’t delete anything from there. You have to add the path “C:\Program Files (x86)\CodeBlocks\MinGW\bin” in that variable. If you are using Windows 10, just click on add new and paste the path there. If you are using Windows 8 or Windows 7, then you have to add a semicolon (;) after what already have been there before and then paste your compiler’s path – “C:\Program Files (x86)\CodeBlocks\MinGW\bin”, then click “OK”. Now open Command Prompt and check if it is installed correctly using ‘g++ –version’ command.
As you have successfully installed you compiler in your system you can compile C programs directly from the Command Prompt. To make your life easier, go to Mingw/bin folder, copy ‘mingw32-make.exe’, paste it in the same folder and rename it to “make.exe”. Now you are ready to compile your code using Makefile. Go to your project folder. Suppose that you have five files: main.c, users.c, users.h, admin.c and admins.h, where the main.c file is the main file of your program. Now write a file named just “Makefile”. Write these lines inside that file:
all: main main: main.o users.o admins.o g++ -o main.exe main.o main.o : main.c g++ -c main.c users.o: users.c users.h g++ -c users.c admins.o: admins.c admins.h g++ -c admins.c clean: del main.exe main.o users.o admins.o
Now again open command prompt and type “cd” then give a space and then type the path of your project’s folder. It will take the Command Prompt to your project’s directory. After that type “make all” and press enter. This command will compile your files.
Here is a demo project with Makefile: http://www.superbcodes.com/wp-content/uploads/2017/03/Demo-Makefile.zip