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.
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
lines 1,2 and 3
Defines variables that will be used in the compiling and the linking. Similar to environment variables.
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.
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.
Build examplemysql.obj target required at line 5.
Build corsshelper.obj target required at line 5.
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