Using levmar with Visual Studio 2010

levmar compilation requires two steps: LAPACK installation and levmar compilation. Here, I use CLAPACK instead of LAPACK for ease of setup but any types of CLAPACK might be fine.

1. CLAPACK installation
There are several options to build CLAPACK. At the following website, you can check and download most suitable file for your system.
Download the file clapack-3.2.1-CMAKE.tgz and expand it. Say the home directory DIR_CLAPACK_SRC = C:\…\clapack-3.2.1-CMAKE .

Setup of CMake is
Where is the source code: DIR_CLAPACK_SRC
Where to build the binaries: DIR_CLAPACK_DST (ex. C:/CLAPACK)
Press the “Configure” button. If the middle window shows, press the button again. Then, press “Generate” button to generate selected build files. If CMake part is successfully finished, output is saved in DIR_CLAPACK_DST.

Open CLAPACK.sln in DIR_CLAPACK_DST with Visual Studio 2010 and build it Build->Batch Build. If the build is successfully finished, the following files are generated in each directories.
lapack.lib: DIR_CLAPACK_DST/SRC/Release
tmglib.lib: DIR_CLAPACK_DST/Testing/MATGEN/Release
libf2c: DIR_CLAPACK_DST/F2CLIBS/libf2c/Release
blas.lib: DIR_CLAPACK_DST/BLAS/SRC/Release
Addition to the lib files, header files are necessary for using levmar.

2. levmar compilation
Once CLAPACK installation finished, you can build levmar. Download the latest (or suitable version) from the official site and expand it.
Similar to CLAPACK installation, you can use CMake to generate sln file.
Where is the source code: DIR_LEVMAR_SRC
Where to build the binaries: DIR_LEVMAR_DST
Depending on your setup, you should manually change some variables of location of lib files as follows
F2D_LIB: DIR_CLAPACK_DST/F2CLIBS/libf2c/Release/libf2c.lib
With correct location setup, you can generate the solution file by pressing “Generate” button. Then, just build the solution file LEVMAR.sln.

3. Test
For simple test can be done with a code as FAQ suggests.
Note that the order of include header files is important. I didn’t check the detail but some website say that clapack.h should be called later.

12 thoughts on “Using levmar with Visual Studio 2010

  1. “Depending on your setup, you should manually change some variables of location of lib files as follows”

    I’m using CMAKE GUI. Where do I change this? Without changing it complies that lapack.lib is not found when LEVMAR is built.

    • Fredrik,
      I quickly compiled with CMAKE + Microsoft Visual Studio 2008 (not 2010) and found out the solution.
      Current CmakeLists.txt assumes that you put all lapack related .lib files into one directory. In Cmake GUI, it’s called LAPACKBLAS_DIR that is pre-defined as ‘/usr/lib’. Even though I don’t understand the reason why Cmake configuration is different from what I did before, my suggestion is to follow the assumption that you put the .lib files into one directory and set LAPACKBLAS_DIR on Cmake GUI. Then, link problem should be solved.

      HOWEVER, I then faced another compilation problem as below:
      levmar.lib(misc.obj) : error LNK2019: unresolved external symbol _sgemm_ referenced in function _slevmar_trans_mat_mat_mult
      levmar.lib(misc.obj) : error LNK2019: unresolved external symbol _dgemm_ referenced in function _dlevmar_trans_mat_mat_mult

      I couldn’t solve the new problem yet but found a discussed at
      If I found the solution, then I will let you.

      • For the above LNK2019 problem, I found the solution on the web.
        First, copy “blaswrap.h” file from CLAPACK directory (CLAPACK/INCLUDE) into levmar directory.
        Next, add “#include “blaswrap.h” at the beginning of “misc_core.c” in LEVMAR.

        With this, I could solve the LNK2019, however, another LNK2019 error happened…

        Finally, I could fully compile levmar by decheck HAVR_LAPACK of Cmake GUI.

  2. Thank you for replying:

    I got around the problem with minor changes in your guide using CMAKE GUI:

    In the second step when LEVMAR was to compiled I did it with Lapack/Blas checked in CMAKE GUI and a minor change in options:
    LAPACKBLAS_DIR to “DIR_CLAPACK_DST/BLAS/SRC/Debug”. Pressed Configure. Generate. And afterwards I copied the of CLAPACK generated blas.lib, libf2c.lib (changed name to f2c.lib), lapack.lib into the build-directory where I put the builded LEVMAR. Then it was possible to build the genrated LEVMAR.sln in Visual Studio.

    However, even if I did build the two files CLAPACK.sln and LEVMAR.sln I’m not able to run the test example expfit.c (

    I copied the texzt in expfit.c pasted it into a new ampty project. To get rid of some errors I exchanged srandom/random to srand/rand and defined M_PI. Then there is no visible errors in Visual Studio (10) but when I compiled I got the same error as I get when I just compile exfit.c without any change:

    “1>LevenbergMarquardt.obj : error LNK2019: unresolved external symbol _dlevmar_der referenced in function _main”

    It looks like it does not find dlevmar_der but right clicking takes me to levmar.h so it finds the corresponding function. I wonder what lib-files/*.h-files/dll-files to include to run expfit.c with or without #define HAVE_LAPACK enabled. Anyone?

    • From lines 106-110, the definition of dlevmar_der is written. That function is double precision LM with Jacobian. And, I found one japanese blog that says we can’t use some routine function when we don’t use lapack.

      From these, I’m not sure but manbe we can’t use dlevmar_der without lapack.

      I’m sure that I could fully compile with clapack previously but I have no idea neither what’s been changed nor what we should do for this…

    • Fredrik!
      Finally, I could compile with LAPACK and also could compile expfit.c!!

      The solution for a link error for sgemm/dgemm is written at levmar FAQ.
      Since I’m using f2c’d BLAS for CLAPACK compilation, I first modified misc.h of levmar:
      1. Uncomment the line 31 of ”
      #define LM_BLAS_PREFIX f2c_”.
      2. Comment out the line 37 of “#define LM_BLAS_SUFFIX _ ”
      3. Uncomment the line 35 of “#define LM_BLAS_SUFFIX”.
      Next, I compiled levmar with HAVE_LAPACK option. Then, the compilation was successfully done.

      After that, I could compile the expfit.c with some modification. What I did was to specify the include file “levmar.h” and library files “blas.lib”, “lapack.lib”, “tmglib.lib”, “libf2c.lib”, and “levmar.lib”.

      What we should do first was to check FAQ!!

      • Hi, I’m sorry that I can’t because I’ve developed everything on Linux for these years 😦 Do you have any specific problem to run the expfit.c?

      • hi, finally can get through internet.
        it’s just that I don’t know how to compile it. and I don’t know what I must change in the vc file so I can compile the expfit.c. but I’m fine with linux though. I actually want to use levmar for curve fitting. so I have a material characteristic data and I know the function but I want to find the parameters of the function I use. and my professor want me to use levmar. and now I’m stuck. I’ll really appreciate it if you help me with this. my professor won’t help me because he thinks I’m not that stuck, but actually I am. sorry if this is out of topic but I’m really desperate. thank you very much.

      • Please tell me where you are and describe the error.

        I guess you successfully installed CLAPACK and levmar. Is this right?

        What do you mean by “I don’t know how to compile it”? It’s like you don’t know how to use visual studio. If you tried to compile expfit.c but failed the compilation, please tell me the error message.

        A potential cause of compilation errors with visual studio is linking error to libraries. We should (1) add a directory, where lib files of levmar are stored, to the link directory and (2) add library files of levmar to the lib files.

      • yes you’re right, visual studio is completely new for me. but since I prefer to use windows so I choose to use it. I’ve compiled the lmdemo but failed to got the result, I couldn’t run lmdemo.exe though it didn’t show any errors that’s why I’m confused. I haven’t managed to compile the expfit.c since I don’t know what to change in the vc file so I could compile it. this is embarrassing, but this is the first time I use vs I’m sorry, I must learn more first.

  3. Pingback: Using levmar 2.6 with Visual Studio | charmie11

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s