René Nyffenegger's collection of things on the web
René Nyffenegger on Oracle - Most wanted - Feedback -
 

About makefiles

Every software developer should know about makefiles and make, at least those concerned with C++. While I don't actually doubt that it's possible to program software without knowing makefiles, I certainly believe it makes the daily business easy. Much easier! In fact, I even think that the process of developping software can be greatly imporoved using makefiles.
make is a software utility that uses a makefile to compile and link source code into an executable file.
There are some software developers that are reluctant to use makefiles. Usually they are used to an IDE (Integrated Development Environment) such as Microsoft Visual Studio, and don't see any benefit in using makefiles. Additionally, makefiles don't look very invitating at first sight. The first makefile I probably saw was an automatically generated one by visual studio and looked something like
65534 65534CPP) @<<
  65534 65534CPP_SWITCHES) 65534 65534SOURCE)
<<


!ELSEIF  "65534 65534CFG)" == "AdpWin - Win32 Debug"

CPP_SWITCHES=/nologo /MDd /W3 /Gm /GX /ZI /Od /I "." /I "...." /I ".." /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ADPWIN_EXPORTS" /D ADPWINDOW_API="__declspec(dllexport)" /D ADPEXCEPTION_API="__declspec(dllimport)" /D ADPHELPER_API="__declspec(dllimport)" /Fp"65534 65534INTDIR)AdpWin.pch" /Yu"adpwinpch.h" /Fo"65534 65534INTDIR)\" /Fd"65534 65534INTDIR)\" /FD /GZ /c 

"65534 65534INTDIR)ADPMsgEraseBackground.obj" : 65534 65534SOURCE) "65534 65534INTDIR)" "65534 65534INTDIR)AdpWin.pch"
	65534 65534CPP) @<<
  65534 65534CPP_SWITCHES) 65534 65534SOURCE)
<<


!ENDIF 

SOURCE=.ADPMsgHandlerADPMsgHandler.cpp

!IF  "65534 65534CFG)" == "AdpWin - Win32 Release"

CPP_SWITCHES=/nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ADPWIN_EXPORTS" /D ADPWINDOW_API=__declspec(dllexport) /D ADPEXCEPTION_API="__declspec(dllimport)" /D ADPHELPER_API="__declspec(dllimport)" /Fp"65534 65534INTDIR)AdpWin.pch" /Yu"ADPWinPch.h" /Fo"65534 65534INTDIR)\" /Fd"65534 65534INTDIR)\" /FD /c 

"65534 65534INTDIR)ADPMsgHandler.obj" : 65534 65534SOURCE) "65534 65534INTDIR)" "65534 65534INTDIR)AdpWin.pch"
	65534 65534CPP) @<<
  65534 65534CPP_SWITCHES) 65534 65534SOURCE)
<<


!ELSEIF  "65534 65534CFG)" == "AdpWin - Win32 Debug"

CPP_SWITCHES=/nologo /MDd /W3 /Gm /GX /ZI /Od /I "." /I "...." /I ".." /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ADPWIN_EXPORTS" /D ADPWINDOW_API="__declspec(dllexport)" /D ADPEXCEPTION_API="__declspec(dllimport)" /D ADPHELPER_API="__declspec(dllimport)" /Fp"65534 65534INTDIR)AdpWin.pch" /Yu"adpwinpch.h" /Fo"65534 65534INTDIR)\" /Fd"65534 65534INTDIR)\" /FD /GZ /c 
When I saw this, I decided not to get into it, it just looked too cryptic to me.
But the thing is, if you can program any computer language, you can create and use makefiles! Makefiles are much easier to understand than a computer language, and once you're used to them, they're even fun! Of course, makefiles you create, will not look ugly and you will understand what's going on.

What is a makefile

In order to build a program, several files are used, that typically are organized in an hierarchycal fashion.
There is an executable (an .exe on windows) which is built from object files (.obj on windows) which, in turn, are compiled from various .c/.cpp and .h files. a makefile lets you write down these dependencies. Bearing this in mind, you can just jump into example one;

Targets

A target is something that is made out of dependencies.
For example, an object file is a target that is built out of .cpp and .h files.

Dependencies

A dependency is used to build a target.
For example, the .cpp and .h files, that are used to build an object file (the target), are this target's dependencies.

Syntax of a makefile

target1:  dep1 dep2 ... depN
<tab>	  cmd1
<tab>	  cmd2
<tab>	  ...
<tab>	  cmdN

target2:  dep4 dep5
<tab>	  cmd1
<tab>	  cmd2

dep4 dep5:
<tab>	  cmd1

Macros

  • $@: the target
  • @<: the first dependency

Suffix rules

.c.o:
	$(CC) $(CFLAGS) -c $< -o $@

nmake

If you're used to developping for microsoft products, you can find an NMAKE reference at msdn

Links

makepp is a drop-in replacement for GNU make which has a number of features that allow for more reliable builds and simpler build files. Written in Perl.