-Visual Studio 2010 which comes with NMake
-levmar 2.6 (http://users.ics.forth.gr/~lourakis/levmar/)
For a recent research project we needed to solve an optimization problem. We considered using levmar by Lourakis. Not having touched C or build code using Make for a while it took a little while to get everything setup and building. In this blog the steps needed will be described. Should you run into trouble please consider the troubleshooting section at the end of this post. If you are interested in using levmar in C# check out this blog post.
In this walkthrough we consider that all code files are in C:\code\levmar-nmake-2.6.
1 - Get Levmar
Download the levmar code and unpack. Move the levmar-2.6 folder to c:\code and rename it to levmar-nmake-2.6. All the content from the tar should now be in the folder C:\code\levmar-nmake-2.6. We chose this name convention to indicate that the original files from levmar are slightly changed for NMake to work. Of course you can ignore this and use the original folder name.
2 - Get CLAPACK
The website of levmar notes that it’s strongly recommended to use lapack for QR factorization. The code can do without, but here the recommendation is followed so lapack needs to be included. For windows there is the free clapack which is described in LAPACK Users' Guide Third Edition. These packages will be used here.
The easiest is to grab windows pre-build packages available here:
Save the packages in C:\code\levmar-nmake-2.6\lapack
3 - Udapte Makefile to use CLAPACK
Open Makefile.vc to update it to use clapack in two places.
First update the LAPACKLIBS_PATH to
Secondly update the lib names by updating
# comment the following line if you are not using LAPACK LAPACKLIBS=clapack.lib blas.lib libF77.lib libI77.lib
# comment the following line if you are not using LAPACK LAPACKLIBS= clapack.lib blas.lib BLAS_nowrap.lib libf2c.lib
4 - Avoid Library Conflicts
Attempt to compile the library in C:\code\levmar-nmake-2.6 using command line command :
nmake /f Makefile.vc
Will result in the following error
LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in MSVCR T.lib(MSVCR120.dll)
The error indicates that two libraries are in conflict using the /NODEFAULTLIB flag for NMake solves this issueupdate the LDFLAGS form:
LDFLAGS= /link /subsystem:console /opt:ref /libpath:$(LAPACKLIBS_PATH) /libpath:.
LDFLAGS= /link /subsystem:console /opt:ref /libpath:$(LAPACKLIBS_PATH) /libpath:. /NODEFAULTLIB:LIBCMT
5 - Use NMake to Compile
compile the library using command line command:
nmake /f Makefile.vc
Should result in:
Microsoft (R) Program Maintenance Utility Version 10.00.30319.01 Copyright (C) Microsoft Corporation. All rights reserved. cl /nologo /I. /MD /TC /W3 /EHsc /D_CRT_SECURE_NO_WARNINGS /O2 /c lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c lm.c Axb.c misc.c lmlec.c lmbc.c lmblec.c lmbleic.c Generating Code... lib /nologo /out:levmar.lib lm.obj Axb.obj misc.obj lmlec.obj lmbc.obj l mblec.obj lmbleic.obj cl /nologo /I. /MD /TC /W3 /EHsc /D_CRT_SECURE_NO_WARNINGS /O2 /c lmde mo.c lmdemo.c cl /nologo lmdemo.obj /link /subsystem:console /opt:ref /libpath:C:\code \levmar-nmake-2.6\lapack /libpath:. /NODEFAULTLIB:LIBCMT /out:lmdemo.exe /increm ental:no levmar.lib clapack.lib blas.lib BLAS_nowrap.lib libf2c.lib
6 - Validate Result LMDEMO
Run the lmdemo application just created by NMake to check if everything is working. You should get somehting like This:
C:\code\levmar-nmake-2.6>lmdemo Covariance of the fit: 0.00483514 -0.00162445 -0.000548114 -0.00162445 0.000546079 0.000184356 -0.000548114 0.000184356 6.22705e-005 Results for Meyer's (reformulated) problem: Levenberg-Marquardt returned 208 in 208 iter, reason 2 Solution: 2.481779 6.181346 3.502236 Minimization info: 1308.25 8.79459e-005 1.14543e-007 7.65876e-030 790.696 208 2 272 21 209
That is it, you are ready to start using levmar!
All the files used here are available for download under GPL:
- The updated Makefile.vc is available here : Makefile (vs2010)
- The levmar source files including changes are available here : levmar-nmake-2.6-vs2010
- The compiled lib (dot.net 4.0 and VS2010) and GPL area available here : build-levmar-2.6-clapack_3.50-net4.0
1 NMake not recognized
Running NMake results in
c:\>nmake 'nmake' is not recognized as an internal or external command, operable program or batch file.
Set the path. For our installation this worked
set path=%path%;C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\
(if using 64-but you might need to point to C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin)
If you get the error
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0 \VC\bin\cl.EXE"' : return code '0xc0000135' You need to run the batch file VCVARS32.BAT to setup the environment. We recommend doing this anyway just to be sure your settings are correct.
Your NMake environment is now setup.
2 - BLAS not BLAS_nowrap
levmar.lib(misc.obj) : error LNK2019: unresolved external symbol _sgemm_ referen ced in function _slevmar_trans_mat_mat_mult
You probably added BLAS not BLAS_nowrap
3 - Reset Project
Delete all the *.obj files. If you don’t they will not be recompiled.