File Exchange

image thumbnail

LBFGSB (L-BFGS-B) mex wrapper

version (79.9 KB) by Stephen Becker
Mex wrapper for lbfgsb v3.0 fortan library. L-bfgs-b solves box-constrained optimization.


Updated 23 Feb 2015

View Version History

View License

----------------------- UPDATE Feb 2015 ----------------------------------------
Due to the difficulty of compiling Fortran for most people (including myself), I have converted the v3.0 L-BFGS-B Fortran code into C (using f2c and some hand-coded changes). I have a new wrapper for it as well; both the C code and the mex wrapper are available at:
You are still welcome to try this code, and if you get it to compile, there will be no major difference in speed or performance. Feel free to leave comments/feedback below. However, I will no longer support this version of the code.
----------------------- END UPDATE ----------------------------------------------

Description: L-BFGS-B is a variant of the well-known "BFGS" quasi-Newton method. This variant uses limited-memory (like L-BFGS), and also handles simple constraints (to be specific, bound constraints, so this includes x >=0 constraints).
The authors of LBFGSB have had fortran implementations available since 1996, but in 2011 they released a major update (v3.0) which has an improved algorithm (you should use this version!). There are existing mex file wrappers for earlier versions, but none worked with v3.0, so I created this file.

This submission tries to make the mex wrapper simple, and then includes a nice .m file solver that requires very few parameters, yet is customizable for advanced users. Run the compile_mex.m file to compile the files. compile_mex.m also includes two quick sanity checks (the file driver1.m is a helper file for one of these checks)

I also have an example of using L-BFGS-B to solve the non-negative least-squares
(NNLS) problem, and comparing it to other existing solvers. To put it simply,
L-BFGS-B is a fantastic NNLS solver, and much better than Matlab's lsqnonneg.


The fortran lbfgsb is at,

There are also versions 2.1 and 2.4 of the library.

For v 2.1, Peter Carbonetto's mex interface works; see and also

For v 2.4 (I don't know where you can find this version though), use the mex files from here:

The wikipedia page (this was mainly edited by one of the authors of the L-BFGS-B algorithm):

For installing on 64-bit Windows, if you need a compiler, see

=== Update, Jan 2015 ===
I am not able to completely support installation/compiling of this toolbox. If you have issues, please mention in the comments below (there are also some solutions from other users in the comments below). Newer versions of Matlab keep changing how files compile (e.g., recent versions do not allow fortran and C to be compiled together), and I am not able to keep up.

If you are using mac or linux, there is a good chance that we can get it to compile. If you are using 64-bit Windows, there is a slim chance you can get it to compile. So please take this as a warning! If anyone is good at software installation and wants to take over the installation script, please do so. The actual wrapper script is quite simple.

Cite As

Stephen Becker (2021). LBFGSB (L-BFGS-B) mex wrapper (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (67)

guanghui liang

Dear Roland & Daniel:
I have installed the LBFGSB according to your instructions, but I encourtered a problem in the last step. When I input the "/cygdrive/d/'Software installation folder'/MATLAB2016/bin/mex.bat -O -largeArrayDims -UDEBUG lbfgsb_wrapper.c liblbfgsb.lib -lmex -lmx -lopenblas", the cygwin output the "Building with 'Microsoft Windows SDK 7.1 (C)'.LINK : fatal error LNK1181:", I hope you can give me some advice to solve it and others who have encourtered this problem can also give me some advice. Thank you very much!

Stephen Becker

Hi @Wanqi, this should work with all versions of Matlab, and you don't need any other toolboxes, but you will need a C compiler. Mathworks has a list of compilers that it is compatible with (and instructions on getting them) at this webpage: Click the "Supported Compilers" link for R2017a, and it will open a PDF. Scroll to the Mac OS section of the PDF, and you'll see that they support Xcode 7 and Xcode 8 (both of which are free, on the Mac App store).

Wanqi Huang

Dear Becker, I have only OS X Yosemite 10.10.5 and MATLAB R2017a, what programs should I download to run the functions? Thank you in advance for your reply.

Stephen Becker

@Rui, I am not exactly sure what you mean, but in general, it's not easy to modify it to handle other types of constraints. It already handles bound constraints

Rui Xiang

Very nice coding! How can I add projected gradient decent into it? e.g after getting x(k+1), I want to project it to the constrained space according to constrations.

Vlad Maximov

weei duan

Ryan Muir

Excellent! The new version of this compiles extremely easily! Thank you very much for updating the code!

Gang Wu

Did a test on my case (bad matrix), lbfgsb improves ~ 10 x in time with minimal setting compared with lsqnonneg. Can I also get solns.m file for a test?

Stephen Becker

@Guillaume, send me an email and I can email you solns.m


Very good! but do you have the solns.m file available for comparison ?


It works quite faster for bigger problem.


It works ! Awesome !

Stephen Becker

I just verified the new C version compiles on 64-bit windows, so I have updated the description above to link to the new github code. I will no longer support this versinon of the code, but I will attempt to support the github version

Stephen Becker

I have a new version that is all in C and should compile MUCH easier, especially on Windows. Please let me know if you have comments. I haven't yet tried compiling on Windows so that may need some fine-tuning.



@Marcin, I think the wrapper works well in Yosemite (this is also my current OS I am using most). You have to make sure you have the updated Xcode and the fortran form Also install the OpenBlas libraries (you will find many guides online) After that, you open MATLAB and set up mex, i.e. mex -setup C++ and mex -setup FORTRAN.

Make sure you have
in your "mex_FORTRAN_maci64.xml" file. Also make sure you copy the downloaded (with gfortran) libgfortran libraries in the folder :

After this, in MATLAB, type

mex -v -c lbfgsb_wrapper.c -largeArrayDims -UDEBUG CFLAGS="\$CFLAGS -O3"

and then,

mex -v -output lbfgsb_wrapper lbfgsb_wrapper.o Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f -lm -lopenblas

Good luck !

Dan Scholnik

Luckily the fix should not measurably affect speed in the most-common case where x is not aliased to another variable. (I'm actually quite fond of writing in-place mex files myself, often to call LAPACK routines directly that matlab does not expose. But of course then I know to be careful. This one took a while to hunt down since it didn't occur to me for a while.)

Stephen Becker

@Dan, good observation and I will include the fix when I next update. For now, the code is true to it's C/Fortran style origins where this practice was commonplace, as it's more efficient for largescale problems. (Back when you told LAPACK how much extra memory you could afford, and saving the entire history of variables was only possible for tiny problems).

Dan Scholnik

The in-place modification of the current iterate x can cause some nasty hair-pulling bugs. If (for example) one is saving x off in a global variable within the objective function, then due to Matlab's copy-on-write optimization the global version will be silently changed as well since Matlab is not aware of the in-place changes. This can be avoided by adding the line

extern bool mxUnshareArray(mxArray *array_ptr, bool noDeepCopy);

at the top of lbfgsb_wrapper.c to declare the undocumented mxUnshareArray function, and then adding the line

mxUnshareArray((mxArray *)prhs[N_x], true);

at the top of mexFunction() before x is overwritten. This forces matlab to perform any delayed copies. Here's a good link with more info:

Stephen Becker

@v, I'm sorry but I can't really give you the help you need to install the 64-bit windows version.


I'm having a hard time compiling this for Max OS X Yosemite. I have MATLAB R2014b. When I run the 'compile_ex' script, I'm getting the following:

Error using mex
Cannot compile both C and FORTRAN source files in a single call to MEX.

Following the advice I was able to find, I then tried:

mex -v -c lbfgsb_wrapper.c -largeArrayDims -UDEBUG CFLAGS="\$CFLAGS -O3" ;

which runs ok, and then:

mex -v -output lbfgsb_wrapper lbfgsb_wrapper.o Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f FC="gfortran" -lm -lmwblascompat32 LINKEXPORT='Wl,--version-script,/usr/local/MATLAB/R2014a/extern/lib/glnxa64/'

which fails:

Error using mex
clang: error: no such file or directory: 'Wl,--version-script,/usr/local/MATLAB/R2014a/extern/lib/glnxa64/'

Can anyone help?


compiled the 64-bit version - when it ran the tests in compile_mex_forWindows.m, it caused an internal error crashing matlab. The crash happens in lbfgsb_wrapper.c at the point it calls setulb


Hi Thomas,

Please download (it's free) the Dependency Walker 64 program and use it to analyze the mex file you created. Dependency Walker will tell you what dll's are missing and causing the "Attempt to access invalid address".
This problem can be solved if you are using Windows 7 64bit. In the case of Windows 8.1 I could not get it to work as the dylib libraries from the cygwin fortran make use of some dll's that are no longer available in Windows 8.1


Thomas Humphries

So I was happily using this code on my 32-bit workstation and now am enjoying trying to get it to work on my 64-bit laptop... I have followed Daniel's instructions below and have managed to get the .mexw64 file to compile, but when I try to run the example I get

Invalid MEX-file 'C:\Users\...\lbfgsb.3.0_1.2\lbfgsb_wrapper.mexw64': Attempt to access invalid address.

Error in lbfgsb (line 179)
[f, task, csave, lsave, isave, dsave] = ...

Error in example_NNLS (line 100)
[xk, ~, info] = lbfgsb(fun, l, u, opts );

Probably a longshot, but does anyone have any suggestion what could be causing this? I am guessing some kind of library linking issue but nothing comes up during the compilation process...



Hi Lucas,

Sorry for my late reply, I haven't checked the posts in a while. I think the problem you describe is quite simple, namely you have to add in the PATH variable in Windows the path to the "lib" command from the Visual Studio folder.


I have received a couple of email asking me how to compile. Here how I proceeds in 2 steps on Linux (Mint 17 distro)

mex -v -c lbfgsb_wrapper.c -largeArrayDims -UDEBUG CFLAGS="\$CFLAGS -O3" ;

mex -v -output lbfgsb_wrapper lbfgsb_wrapper.o Lbfgsb.3.0/lbfgsb32.f Lbfgsb.3.0/linpack32.f Lbfgsb.3.0/timer.f FC="gfortran" -lm -lmwblascompat32 LINKEXPORT='Wl,--version-script,/usr/local/MATLAB/R2014a/extern/lib/glnxa64/'

It is a colleague of mine who found out how to compile, therefore I am afraid I cannot explain why it works.


I have the same problem as Alexandre. When I try to compile it under Linux with matlab R2014a, I get the following error:
Error using mex
Cannot compile both C and FORTRAN source files in a single call to MEX.

Error in compile_mex (line 61)
mex -v lbfgsb_wrapper.c -largeArrayDims -UDEBUG ...

Anyone had meet this problem? Please kindly show me the solution, I'm really in trouble.


Problem sloved. It seems that Matlab R2014a does not allow to compile Fortran and C in a single MEX call. I believe that it allowed to link the files automatically.

I had to compile the Fortran code to .o objects and link them to the .c files manually.


Hi Stephen,

I managed to compile the Fortran part by splitting into 2 MEX calls:

mex Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f -lm -lmwblas CFLAGS="\$CFLAGS -O3"FC="gfortran"

mex -v lbfgsb_wrapper.c -largeArrayDims -UDEBUG ;

The Fortran part is fine, but not the C compilation. I get the following error message:

Error using mex
/tmp/mex_29407351995475_5199/lbfgsb_wrapper.o: In function `mexFunction':
lbfgsb_wrapper.c:(.text+0x738): undefined reference to `setulb_'
collect2: error: ld returned 1 exit status

I have tried to play with the -DNOUNDERSCORE -UUPPERCASE_FORTRAN flags but none of them seem to help.

How can I fix this issue?

Thank you very much

Stephen Becker

Hi Alexandre,
I've never seen that error either. Maybe ask the mathworks help about it.


I have tried to compile it under Linux Mint 17, with Matlab 2014a. I got the following error message:

Error using mex
Cannot compile both C and FORTRAN source files in a single call to MEX.

Error in compile_mex (line 61)
mex -v lbfgsb_wrapper.c -largeArrayDims -UDEBUG ...

I've tried to find out about this error but it is nowhere to be found, as if it is an issue related to Matlab 2014 only.

Any suggestion?


Hi everyone,

As I'm trying to compile the mex file on a W7 x64 with Matlab R2013a, I follow the tutorial of Daniel, and I would need some specification (thanks by the way for sharing this) :
- the line "lib /def:liblbfgsb.def /machine:x64" gives me
$ lib /def:liblbfgsb.def /machine:x64
-bash: lib: command not found

-I tried to run the compilation anyway, and it gives me this :

$ mex.bat -O -largeArrayDims -UDEBUG lbfgsb_wrapper.c liblbfgsb.lib -lmex -lmx -lopenblas

C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: 'lbfgsb_wrapper.c' not found.

I'm not a great expert on bash's command, and I can't figure out what's the real problem behind these.




I succeded to compile and test the mex module for Windows 7 x64 (Sp1) linking with the multithreded OpenBlas. In this way one can take advantage of the multicore architecture of the available CPU. For the Windows operating system the process is rather convoluted and delicate (one can easily get into the so called "dll hell" problem). The solution is indeed based on creating a dll file. I got it working following a script from Roland, so many thanks to him who pioneered a way to compile using Cygwin and gracefully shared his own script.
The idea is the following:
1) install Openblas and add its lib folder to the $Path$ Environment variable.
2) install Cygwin64 (the Devel package)
3) make sure you have SDK 7.1 installed along with Visual Studio 10.0 (it's free) and all the updates for Win7.
4) add to the $Path$ environment variable "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64"
5) find and copy the x64 version of IESHIMS.dll into a folder added to $Path$ environment variable. (I know, this is counterintuitive and seems esoteric but if you don't it may not work) To check that this is needed one can use "Dependency Walker".
6) open the Cygwin64 terminal and go to the "lbfgsb3.0_mex1.2" folder and make sure the "Lbfgsb.3.0" subfolder with all its files is inside (if not download it).
7) Type (or creat a .sh script to run at once):
cd Lbfgsb.3.0
rm *.o
rm *.dll
rm *.lib
rm *.dll.a

x86_64-w64-mingw32-gfortran.exe -c -Wall -Wno-uninitialized lbfgsb.f linpack.f blas.f timer.f
echo EXPORTS > liblbfgsb.def
echo setulb_ >> liblbfgsb.def
echo _setulb_=setulb_ >> liblbfgsb.def

dlltool -D liblbfgsb.dll -d liblbfgsb.def -e liblbfgsb.o -l liblbfgsb.dll.a lbfgsb.o linpack.o blas.o timer.o
lib /def:liblbfgsb.def /machine:x64

x86_64-w64-mingw32-gfortran.exe -mdll -static-libgfortran -o liblbfgsb.dll liblbfgsb.o lbfgsb.o linpack.o timer.o blas.o

cp liblbfgsb.dll liblbfgsb.dll.a liblbfgsb.lib .
cp liblbfgsb.dll liblbfgsb.dll.a liblbfgsb.lib ..
cp /usr/x86_64-w64-mingw32/sys-root/mingw/bin/libgcc_s_seh-1.dll ..
cd ..

/cygdrive/c/Progra~1/MATLAB/R2013b/bin/mex.bat -O -largeArrayDims -UDEBUG lbfgsb_wrapper.c liblbfgsb.lib -lmex -lmx -lopenblas

8) Test example_NNLS.m

Good Luck!


Dear Chenchen Tong,

I got the same error, when running compile_mex.m on Ubuntu 12.04, gcc-4.6 and MATLAB R2013a:

=== lbfgsb "driver1" test problem, 2D ===
Invalid MEX-file
'.../lbfgsb.3.0_1.2/lbfgsb3.0_mex1.2/lbfgsb_wrapper.mexa64':'.../matlabR2013a/bin/glnxa64/../../sys/os/glnxa64/ version
`GFORTRAN_1.4' not found (required by

For me the problem was solved by running MATLAB with LD_PRELOAD and LD_LIBRARY_PATH, pointing to the system libraries:

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/ LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu matlab

Ryan Muir


Hi Roland,

Could you please post detailed instructions for how to compile the lbfgsb_wrapper in Windows 7 64bit?
I was only able to compile in Linux and Mac OS X so far . I also linked with the OpenBlas library which is very fast ( if you have a multi-core cpu).

Thank you very much.


Stephen Becker

Hi Roland,
That's great it works! If you wouldn't mind, can you post detailed instructions here? I think many others would find them useful.

You're right about the typo in the nrhs. I'll fix that in the next update.

Right now, the gradient is necessary. The Python and R versions use large packages to get around this. They might have a finite-differences gradient approximation mode, but I suspect they are really doing automatic differentiation (the good news is that this is the exact gradient, no approximation. The bad news is that it can be sometimes slow). There are automatic differentiation packages for Matlab, but none that seem to be very "mature". So you could try some of those, but if you can work out an explicit function to compute the gradient, that is usually the simplest (no automatic differentiation packages to get working) and the fastest for computation.
Best, Stephen


I finally managed to run it on Win7 64 with mingw fortran and msvc c compilers.

Stephan, is it necessary to give the gradient in any case? I know that the Python and R versions can use numerical approximations to the gradient. Do you know of a function that does that for me?


Thanks Steven, I'm still trying to get this to work. At the moment I got it compiled but it crashes when I run it with no expressive error messages. Still debugging... but on a different not, in line 266:

if (nrhs < N_g-1 ) {

shouldn't it read "nrhs < N_g+1", so a "+" instead of "-"?


Stephen Becker

Hi Roland,
I think that's with trying the f2c'd version of the code. It might be that your C compiler is stricter than the f2c code. Maybe you can tell the compiler to allow a newer standard? In gcc, it would be something like -std=gnu99 or -ansi. You can probably look up the equivalent for MSVC.
Or, if you can get a fortran compiler, then use the original fortran code and don't mess with the f2c version.
Either way, I'm afraid I can't help much because I work with a Windows computer. Judging from the other comments here, it is quite difficult to get this code to work with 64 it Windows. We really need a pure C version of the code; if you want to do this (you can use the f2c version as a base), please let me know!


Dear Stephen,

Thanks for all your efforts. I would love to use your script but it doesn't compile. I have Matlab R2012b on Win 7 64bit. I use the compile_for_windows script and get the following error:


>> compile_mex_forWindows
Compiling abort_.c
Compiling arithchk.c
Compiling backspac.c
Compiling c_abs.c
Compiling c_cos.c
c_cos.c(14) : error C2143: syntax error : missing ')' before '*'
c_cos.c(14) : error C2143: syntax error : missing '{' before '*'
c_cos.c(14) : error C2143: syntax error : missing ';' before '*'
c_cos.c(14) : error C2059: syntax error : ')'
c_cos.c(16) : error C2054: expected '(' to follow 'z'

C:\PROGRA~1\MATLAB\R2012B\BIN\MEX.PL: Error: Compile of 'c_cos.c' failed.

Error using mex (line 206)
Unable to complete successfully.

Error in compile_mex_forWindows (line 116)


Any suggestions how to fix that would be very much appreciated.



Stephen Becker

Hi Xin,
You are using the f2c version I included, so this adds more complication. The problem is almost surely that your BLAS requires integers of different lengths. So try two things:
First, define _BLAS64_ when you compile. You can do this by adding -D_BLAS64_ to the mex compilation command. Hopefully this will fix it.
Second, try a different BLAS. You can install another version (e.g. ATLAS) and use that. If there are still problems, use the netlib source code (for CBLAS) and link with this source; this way your compiler will give you errors if there are incompatibilities and they will be easy to fix.
Best, Stephen

xin yu

Hi, Stephen
Thank you for the wrapper, which can work pretty well on my windows7 32bit.
But when compiling the wrapper on my desktop, win7 64bit, I met some problems.
I can compile the wrapper, but when running the examples, matlab will crash. I debug the problem. In the loop, when calling the lbfgsb.c at the first time, it is ok, but the second iteration, a breakpoint is triggered. When calling dcopy, matlab will crash down. I have no idea how can I solve the problem. My BLAS is 10.3.11. My matlab version is R2013a.

Stephen Becker

I'm not sure what's going on. It's odd that it compiles if it can't find the symbol. It could be an issue with a run-time library. On linux, I had the option of using either g95 or gfortran, and I use g95, so maybe try that if it's easy on the mac. Otherwise, I'm afraid I can't be of much help. You could also try following the basic ideas outline in the compile_mex_forWindows (i.e. convert fortran to C). Best of luck!


My apologies for the numerous replies. I had to manually update dcopy dscal and daxpy as you suggested. The mex file is generated ("lbfgsb_wrapper.mexmaci64") but it crashes matlab. The error report says: "I should have added that the error report listed "Symbol not found: __gfortran_transfer_real_write""


I should have added that the error report listed "Symbol not found: __gfortran_transfer_real_write"

Stephen Becker

Hi Tim,
The crash, the fact that the mwblas seemed to work (for compilation), and the fact that it was looking for _daxpy_ and not _daxpy32_, all suggest that maybe the ddot's and so on were not replaced by ddot32. The C files should be OK, so check the fortran files. You can try running the script from the shell yourself, or just edit the .f files with an editor and do find-replace.

If you verify that the ddot/dcopy/dscal/daxpy are replaced with their ddot32/dcopy32/dscal32/daxpy32 and it still fails, then we have a different problem on our hands. Try compiling with -v and let me know the output.


Thank you for writing this code. I hope to be able to use it, but I am having some compilation problems. I am running a 64 bit mac, OS 10.6.8, running Matlab v. R2010b.

I get the error "script failed, try running script by hand perhaps?," though the files are still generated with ddot32 replacing dot. It does not, however, compile when using the subsequent mex command ("can't find "_daxpy_" etc). It did compile using the previous, commented out mex command using lmwblas, and generated a mexmaci64 file. If I try running the example code calling lfbgs, Matlab crashes.

The libblas version is 10.2.3 for R2010b. If you could offer any solutions I would be grateful,

Stephen Becker

Hi Xunkai, I can't help much because I don't have access to Intel's compiler. But if you adapt the gfortran lines to work with the intel fortran compiler, then the rest of the compilation (the C code) should be similar.

Xunkai Wei

Win 7, 64bit system, I have intel fortran compiler and vs 2012 C++ compiler, could you show me how to compile the source code ?

Ernst Kloppenburg

Stephen Becker

Hi Chris, I must have missed that file with my last update; thanks for noticing. I have included it now and the update should be on the web in the next day.

Chris Barber

Hi Stephen,

I can't seem to find compile_mex_forWindows.m in your file submission. Would you be able to upload it?


Thanks a lot for this wrapper. Works beautifully on my UBUNTU 64 bit system :)


Couldn't get it to run despite trying all options including the f2c. Compiles and crashes when I try to run an example. I'm running OS X 10.6, Matlab 2011a, 64 bit, with gcc 4.6.2 (prerelease) and Blas 10.2.6. Could it be that my compiler is too new? Matlab documentation says I need an older gcc, 3.2, and older gfortran, 4.3.x.

Georg Altenstein

Thanks a lot for the wrapper.

Stephen Becker

I have updated the package to include a new install script that should work for 64-bit BLAS, and it works for Vicenc's case at least.

Vicenç Gomez

Vicenç Gomez

Dear Stehpen,

thanks for your fast answer. The different versions of libblas I tried with are:

for R2009a : 10.1.1
for R2011b : 10.3.2
for R2012a : 10.3.5

Intel(R) Math Kernel Library Version 10.1.1 Product Build 082212.12 for Intel(R) 64 architecture applications

Intel(R) Math Kernel Library Version 10.3.2 Product Build 20110112 for Intel(R) 64 architecture applications

Intel(R) Math Kernel Library Version 10.3.5 Product Build 20110720 for Intel(R) 64 architecture applications

Stephen Becker

Hi Vicenc, I'm sorry to hear about the 64-bit problems. I don't think it's an issue with the compiler, but rather it's probably a BLAS issue. Can you run "version -blas" in Matlab and let me know what it says? I might be able to supply an update in a few days that helps these 64-bit issues, but I need to be able to recreate the problem first.

Vicenç Gomez


I'm using the wrapper in the laptop using ubuntu 32bit. I works perfectly.

However, as desktop machine I have a 64bit processor and both tests crash. I can compile successfully and the crashing point is arbitrary: sometimes at iteration 4, sometimes in the beginning, depending on the Matlab version.

I tried with R2009b, R2011b, R2012a and always crashes. I also tried with both 4.4 and 4.3 gfortran and gcc. What could be the problem?

I'm using:

Intel(R) Core(TM)2 Duo CPU E8500 @ 3.16GHz
Linux 2.6.32-41-generic #90-Ubuntu SMP Tue May 22 11:29:51 UTC 2012 x86_64 GNU/Linux

Stephen Becker

Hi Okan,
The L-BFGS-B software calls the BLAS library to perform most of its linear algebra, and unfortunately, the conventions for BLAS have changed over time. I suspect that is the problem.
To get some more information, run this command in both versions of Matlab:

version -blas

If you have different versions of BLAS, that could be the issue. You could try running R2011b using the BLAS library that R2011a uses. To change the BLAS library, export the environment variable BLAS_VERSION to point to the location of the BLAS library (on linux, it might look like:

export BLAS_VERSION=/opt/matlab-R2011a/bin/glnxa64/ )

Hope that helps. If you have more issues, please email me.

Okan Irfanoglu

First of all, thanks for this wrapper.

I had a quick question. When I compile everything with gcc/gfortran 4.3.3 and call the mex file from Matlab 2011a, everything works fine and both tests succeed.

However, when i use the same compilers and Matlab R2011b, matlab crashes in the tests. The two compilers are still supported by this matlab as well so I wanted to ask you if you had any thoughts about this.

Thank you.


I am trying to compile on a 32 bit Windows 7 machine, running Matlab 2010a. Supposing that I have the f2c library correctly compiled could you try and give me some suggestions on how to rewrite the mex function call within compile_mex.m?

This is what I am currently trying:
mex lbfgsb_wrapper.c -largeArrayDims -DDEBUG ...
Lbfgsb.3.0/lbfgsb.f Lbfgsb.3.0/linpack.f Lbfgsb.3.0/timer.f ...
-lm -lblas -lvcf2c -LC:\Users\Mike\Documents\MATLAB\libf2c ...

I get a long series or errors which appear to be complaining about syntax:

Error lbfgsb_wrapper.c: 422 illegal statement termination
Error lbfgsb_wrapper.c: 422 skipping `int'
Error lbfgsb_wrapper.c: 422 undeclared identifier `ndim'
Error lbfgsb_wrapper.c: 423 illegal use of type name `mwSize' ..........

I have a feeling that I may need to change something in my mexopts.bat file. Not sure where to start though. Any help/suggestions would be greatly appreciated! Thanks.

Stephen Becker

Another comment: on Windows, you need a fortran compiler, and might need to do some fiddling around. But it is possible because someone did it successfully; email me if you want more help. But in general, it is difficult on windows. It might be best to use f2c.


I was having the same problem with 2012a. The error gets fixed if you install gcc-4.4,g++-4.4,gfortran-4.4 and change the default compilers in to these.

Chenchen Tong

hi,Stephen, thank a lot for your brilliant work. This is just what I need now. I am trying to use this optimizer on image registration. However, I am not able to make it work. I compile with the mex file on 64bit ubuntu 11.1 with matlab 2011b. The GCC version is 4.6.1. I managed to make the mexa64 file. But when running the test 1, I got error like:
=== lbfgsb "driver1" test problem, 2D ===
Invalid MEX-file '/media/Data/project/codes/Lbfgsb.3.0/lbfgsb_wrapper.mexa64':
version `GFORTRAN_1.4' not found (required by

Error in lbfgsb (line 174)
[f, task, csave, lsave, isave, dsave] = ...

could you kindly help me with this? Thanks

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!