Nmake Makefile Tutorial and Example

Aim
The aim of this Nmake C++ tutorial is to give a quick introduction to nmake. We will look at the basics of an nmake makefile and go through the options of compiling and linking. Microsoft's C++ (Visual Studio) nmake is very similar to gnu's make with a few minor differences. Nmake has options very similar to gnu make and we will use some of them in this example. Leave any questions or problems as comments and I will endeavour to answer them.

Assumptions
This article assumes that you have a compatible version of Visual C++ installed. For this example we used Visual C++ 2008 Express Edition. However, there should not be any significant issues.

As an example we will use a makefile that is used to compile one of the examples in this website - the common database pool template.


When you run nmake it looks for a file called makefile in the current directory. You can specify a file using the nmake -f myfile option.

While it may look like cl.exe is doing all the work, cl.exe does the compiling and link.exe does the linking. For all the compile options you can use cl /? and for all the linker options you can use link /?
You can run nmake without a target or you can specify a target such as nmane clean. Without a target nmake will look for the first available target in the makefile and build it.

 1. MYSQL_HOME=D:\downloads\mysql-connector-c++-noinstall-1.0.5-win32
 2. MYSQL_INCS=$(MYSQL_HOME)\include
 3. MYSQL_LIBS=$(MYSQL_HOME)\lib
 4. 
 5. mysqlbuild: mysqlpool.obj examplemysql.obj crosshelper.obj
 6.   cl /o hello.exe mysqlpool.obj examplemysql.obj crosshelper.obj /link /LIBPATH:$(MYSQL_LIBS) mysqlcppconn.lib
 7. 
 8. 
 9. examplemysql.obj: ExampleMySQL.cpp
10.     cl /c ExampleMySQL.cpp /D WINDOWSXX -I $(MYSQL_INCS)
11. 
12. mysqlpool.obj: ..\mysql\MySQLPool.cpp 
13.     cl /c  ..\mysql\MySQLPool.cpp /D WINDOWSXX  -I $(MYSQL_INCS)
14. 
15. crosshelper.obj: ..\CrossHelper.cpp
16.     cl /c ..\CrossHelper.cpp /D WINDOWSXX 
17. 
18. oraclebuild:
19.     echo "oracle build goes here"
20. 
21. all:oraclebuild mysqlbuild
22. 
23. clean:
24.   del *.exe *.obj
Hide line numbers


lines 1,2 and 3
Defines variables that will be used in the compiling and the linking. Similar to environment variables.

line 5
Defines a target called mysqlbuild that depends on other targets mysqlpool.obj, examplemysql.obj and crosshelper.obj. Before you goto line 6 to do the build nmake has to make sure that the dependencies are built.

line 12,13
Here it finds the target mysqlpool.obj required at line 5. It depends on the CPP source file MySQLPool.cpp so now we go to the next line to compile the file. This is a compile only /c option. As you can see we are using the /D WINDOWSXX to define the WINDOWSXX macro and -I $(MYSQL_INCS) which is specifying the directory to search for include files.

line 9,10
Build examplemysql.obj target required at line 5.

line 15,16
Build corsshelper.obj target required at line 5.

line 6
Once all the dependencies are built come back to line 6 and build the target. Here we are using /link to give the linker options which in this case is the LIBPATH to the mysqlcppconn.lib

You can try nmake oraclebuild which will just display a message, and nmake all will try to build both the oraclebuild and mysqlbuild targets.

Running nmake clean will delete all the *.obj and *.exe files so you can build them again.

Back to the tutorial trail | Home

5 comments:

Sergey said...

You beauty! This was just what I was looking for. At least I now know how to supply the linker options!

Matt said...

Thanks! This is just what I needed to get started with nmake on some small projects

Guti said...

Quite self explanatory.
Thank you.

Anonymous said...

awesome, thanks!

Anonymous said...

a COOL tutorial indeed, just what i was looking for.

Thanks man.