Compiling Levmar using NMake (Visual Studio 2010)

Prerequisites:

-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.

Setup

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:

http://www.netlib.org/clapack/LIB_WINDOWS/prebuilt_libraries_windows.html

download:

  1. clapack.lib
  2. blas.lib
  3. BLAS_nowrap.lib
  4. libf2c.lib

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

LAPACKLIBS_PATH=C:\code\levmar-nmake-2.6\lapack

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

To

# 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:.

to

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


image00

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

image01

That is it, you are ready to start using levmar!

 


All the files used here are available for download under GPL:


TROUBLESHOOTING

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

Problem

levmar.lib(misc.obj) : error LNK2019: unresolved external symbol _sgemm_ referen

ced in function _slevmar_trans_mat_mat_mult

Solution

You probably added BLAS not BLAS_nowrap

 

3 - Reset Project

Delete all the *.obj files. If you don’t they will not be recompiled.

10 thoughts on “Compiling Levmar using NMake (Visual Studio 2010)

    1. Ps

      Please post you .net version and visual studio version and any error messages you get so I can try to reproduce your issue.

      Kind regards,

      Jorrit

      1. Hi Jorrit,
        I use VS 2010 and .NET 4.0. There's no error messages, when I tried to run lmdemo.exe it didn't work. I waited for about 2 hours and nothing happen. I've tried the the levmarsharp solution you gave but I still couldn't run lmdemo.exe. By the way, is there any lecture video for this? I couldn't find anything in youtube. Thank you very much.

        Regards,

        Hida.

      2. PS
        I got another errors while building levmarsharp:

        |Warning 1 warning C4635: XML document comment applied to 'LevmarSharp.LevmarJacf': badly-formed XML: Invalid at the top level of the document. e:\levenberg_marquardt\levmar_sharp\ls\levmarsharp\levmarsharp\LevmarSharp.h 21 1 LevmarSharp|

        and this:

        |Error 2 error LNK1123: failure during conversion to COFF: file invalid or corrupt E:\Levenberg_Marquardt\levmar_sharp\LS\LevmarSharp\LevmarSharp\LINK LevmarSharp|

        I googled it and they said I should update to VS2010SP1 but I couldn't. What should I do?
        thanks.

        Regards,

        Hida.

    2. Hi Hida,

      If you are having trouble with the linker SP1 is probably the easiest solution, but you could also give this suggestion on Stack Overflow a go (http://stackoverflow.com/questions/10888391/error-link-fatal-error-lnk1123-failure-during-conversion-to-coff-file-inval).

      Do you need to compile the code yourself? If you just want to use the solver in Excel you can also grab the compiled code plus Excel/VBA example here https://uglyduckling.nl/software/levmarexcel/. If you need to have the solver in C# have a look here https://github.com/AvengerDr/LevmarSharp.

      Regards,

      Jorrit

      PS

      If you want I am happy to loo at you code

      1. Hi Jorrit,
        I haven't made the code, because from the start I had the trouble compiling it. I use levmar for curve fitting and still don't understand how to use it yet.
        I need to compile the code myself. I will show you my code after make it :).
        For now, I will try your solution. Thank you very much and I'm sorry if I gave you troubles :).

        Regards,

        Hida.

  1. Hello Jorrit,
    I think I'm really stuck now, could you please help me how to use levmar for curve fitting?
    Especially about the library parts?

    Regards,

    Hida.

    1. Hi Hida,

      Happy to help. Copy and past some code snippets of your code to show me where you are stuck. Or send me your code in email so I can have a look.

      Regards,

      Jorrit

Leave a Reply

Your email address will not be published. Required fields are marked *