Unable to compile using "mex fmpc_sim.c libmwblas.lib libmwlapack.lib" undefined reference to '_dgemm_'
    4 views (last 30 days)
  
       Show older comments
    
I am having the exact same problem as https://www.mathworks.com/matlabcentral/answers/172057-error-using-mex-line-206-unable-to-complete-successfully
I installed matlab2011a (32 bit) in windows10 (64bit).
I tried to install Fast Model Predictive Control Using Online Optimization from http://stanford.edu/~boyd/fast_mpc/ which contain a Mex solver. During the setup step, I am supposed to compile a c file fmpc.c using
mex fmpc_sim.c libmwblas.lib libmwlapack.lib
But for whatever reason, it says there are a bunch of undefined references. I have followed the instructed nearly exactly as stated, what could be going wrong? How do I solve this reference error?
EDU>> mex -setup
Welcome to mex -setup.  This utility will help you set up  
a default compiler.  For a list of supported compilers, see  
http://www.mathworks.com/support/compilers/R2011a/win32.html 
Please choose your compiler for building MEX-files: 
Would you like mex to locate installed compilers [y]/n? y
Select a compiler: 
[1] Lcc-win32 C 2.4.1 in C:\PROGRA~2\MATLAB\R2011A~1\sys\lcc 
[0] None 
Compiler: 1
Please verify your choices: 
Compiler: Lcc-win32 C 2.4.1 
Location: C:\PROGRA~2\MATLAB\R2011A~1\sys\lcc 
Are these correct [y]/n? y
Trying to update options file: C:\Users\n\AppData\Roaming\MathWorks\MATLAB\R2011a\mexopts.bat 
From template:              C:\PROGRA~2\MATLAB\R2011A~1\bin\win32\mexopts\lccopts.bat 
Done . . . 
************************************************************************** 
  Warning: The MATLAB C and Fortran API has changed to support MATLAB 
           variables with more than 2^32-1 elements.  In the near future 
           you will be required to update your code to utilize the new 
           API. You can find more information about this at: 
           http://www.mathworks.com/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9 
           Building with the -largeArrayDims option enables the new API. 
************************************************************************** 
EDU>> mex fmpc_sim.c libmwblas.lib libmwlapack.lib
Writing library for fmpc_sim.mexw32 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dgemm_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dposv_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dgemv_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_daxpy_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dtrtrs_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dtrsv_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dnrm2_' 
    C:\PROGRA~2\MATLAB\R2011A~1\BIN\MEX.PL: Error: Link of 'fmpc_sim.mexw32' failed. 
??? Error using ==> mex at 208
Unable to complete successfully.
Installation instructions for fast_mpc -----------------------------------------------------------------------
fast_mpc is written in C with a mex interface to MATLAB. Before installation, make sure that BLAS and LAPACK libraries are installed in your machine.
1. Unpack the fast_mpc files, or get them individually.
2. Start MATLAB and cd to the directory containing the source files.
3. At the MATLAB command prompt type
        >> mex -setup
   and enter the number corresponding to the template option gccopts.sh.
4. Compile fmpc_sim.c using
        >> mex fmpc_sim.c
   On some machines you may need to specify the libraries
        >> mex fmpc_sim.c -lblas -llapack
   If you want to link your own libraries, you can. For example,
   to link the libraries libacml.a, libacml_mv.a, and libgfortran.a 
   in the directory /opt/acml/lib, use the option '-L' to specify the 
   library search path and the option '-l' to specify the individual 
   libraries, as in
       >> mex fmpc_sim.c -L/opt/acml/lib -lacml -lacml_mv -lgfortran
5. If fmpc_sim.c compiles successfully, do the same with fmpc_step.c.
6. Test the installation by running the masses_example script
       >> masses_example
   which will run MPC, for 100 iterations, on the masses example in the paper,
   using mpc_step.  You should see reports from each iteration printed to your 
   screen. The last one, for example, should look like
 iteration       step            rd                      rp
    0            0.3874          1.09294e+01             1.27108e+00
    1            1.0000          1.54052e+00             1.39748e-14
    2            1.0000          2.98332e-01             1.40216e-15
    3            1.0000          1.16741e-02             1.02482e-15
The average cost is: 5.683200
   Test mpc_sim by running the script randsys_example.
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 23 Oct 2016
        mex -Ddgemm_=dgemm fmpc_sim.c -lmwblas -lmwlapack
or
mex -D_dgemm=dgemm fmpc_sim.c -lmwblas -lmwlapack
depending which side the underscore is showing up on. Repeat similar -D for the other symbols.
6 Comments
  Debashish Mohapatra
 on 10 Jan 2018
				Same here. The library was compiled after adding the recommended definitions by James Tursa, and can be successfully compiled by following the suggestions made by Walter Roberson i.e.
mex -D_dgemm=dgemm fmpc_sim.c -lmwblas -lmwlapack
However, running the given examples with the library crashes MATLAB.
  Bernardo Hernandez
 on 20 Jun 2019
				
      Edited: Bernardo Hernandez
 on 20 Jun 2019
  
			Same problem here. I tried James Tursa's answer and it compiles without problems. But then, MATLAB simply closes when I try to run the examples.
I also compiled it on Linux, which was possible without any modification of the original files. This further confirms that the issue is due to function calling style. Nevertheless, when I called the linux-compiled functions MATLAB also crashed.
Has anyone found a solution to this?
More Answers (1)
  James Tursa
      
      
 on 23 Oct 2016
        Looks like the source code you are using was intended for UNIX machines. In particular these errors:
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dgemm_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dposv_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dgemv_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_daxpy_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dtrtrs_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dtrsv_' 
c:\users\n\appdata\local\temp\mex_89dw4r\fmpc_sim.obj .text: undefined reference to '_dnrm2_'
The extra underscore _ at the end of the BLAS library names indicate UNIX convention names, but you are compiling on a Windows machine and these trailing underscores are not present in the names in the Windows versions of these libraries. The solution is simply to get rid of those extra underscores at the end of these names since they are not present in the Windows library versions. You could edit your code to change all of these names, or maybe all it will take is to put something like this in your code so it will use the correct names in either version:
#if defined(__OS2__) || defined(__WINDOWS__) || defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) || defined(_MSC_VER)
#define  dgemm_  dgemm
#define  dposv_  dposv 
#define  dgemv_  dgemv
#define  daxpy_  daxpy 
#define  dtrtrs_ dtrtrs  
#define  dtrsv_  dtrsv 
#define  dnrm2_  dnrm2
#endif
4 Comments
  Ankitkumar Maddewad
 on 1 Mar 2021
				Facing similar issue. How can we resolve the issue of MATLAB crashing?
Thanks in advance.
See Also
Categories
				Find more on Introduction to Installation and Licensing in Help Center and File Exchange
			
	Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!









