About mex function problem
Show older comments
Hi everyone,
I recieved the code using mex function. When I run it on Matlab, it occured errors as below
Error using mex
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x0):
multiple definition of `vsnprintf'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x3b0): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x10):
multiple definition of `getc'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x3c0): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x40):
multiple definition of `putc'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x3f0): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x70):
multiple definition of `getchar'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x420): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0xa0):
multiple definition of `putchar'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x450): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0xe0):
multiple definition of `fopen64'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x490): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0xf0):
multiple definition of `ftello64'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x4a0): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x120):
multiple definition of `vsnwprintf'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x4d0): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x130):
multiple definition of `strtof'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x4e0): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x150):
multiple definition of `wcstof'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x500): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x170):
multiple definition of `_Exit'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x520): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x180):
multiple definition of `llabs'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x530): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x1a0):
multiple definition of `atoll'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x550): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x1b0):
multiple definition of `lltoa'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x560): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x1c0):
multiple definition of `ulltoa'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x570): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x1d0):
multiple definition of `wtoll'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x580): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x1e0):
multiple definition of `lltow'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x590): first defined here
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\c_mexapi_version.obj:c_mexapi_version.c:(.text+0x1f0):
multiple definition of `ulltow'
C:\Users\BAO_MA~1\AppData\Local\Temp\mex_85703934924329_1184\lbfgsC.obj:lbfgsC.c:(.text+0x5a0): first defined here
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libm.a when searching for -lm
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmingw32.a when searching for -lmingw32
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmoldname.a when searching for -lmoldname
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmingwex.a when searching for -lmingwex
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmsvcrt.a when searching for -lmsvcrt
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libuser32.a when searching for -luser32
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libkernel32.a when searching for -lkernel32
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libadvapi32.a when searching for -ladvapi32
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libshell32.a when searching for -lshell32
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmingw32.a when searching for -lmingw32
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmoldname.a when searching for -lmoldname
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmingwex.a when searching for -lmingwex
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
skipping incompatible C:/HEN_HOUSE/gnu/lib\libmsvcrt.a when searching for -lmsvcrt
C:/ProgramData/MATLAB/SupportPackages/R2019b/3P.instrset/mingw_w64.instrset/bin/../lib/gcc/x86_64-w64-mingw32/6.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
i386 architecture of input file `C:/HEN_HOUSE/gnu/lib/dllcrt2.o' is incompatible with i386:x86-64 output
C:/HEN_HOUSE/gnu/lib/dllcrt2.o: In function `__dll_exit':
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:140: undefined reference to `_free'
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:148: undefined reference to `_fflush'
C:/HEN_HOUSE/gnu/lib/dllcrt2.o: In function `DllMainCRTStartup':
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:87: undefined reference to `_DllMain@12'
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:56: undefined reference to `_malloc'
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:67: undefined reference to `__pei386_runtime_relocator'
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:74: undefined reference to `___main'
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:59: undefined reference to `__errno'
C:/HEN_HOUSE/gnu/lib/dllcrt2.o: In function `atexit':
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:166: undefined reference to `___dllonexit'
C:/HEN_HOUSE/gnu/lib/dllcrt2.o: In function `_onexit':
d:/src/mingw/build/runtime/../../runtime/dllcrt1.c:182: undefined reference to `___dllonexit'
collect2.exe: error: ld returned 1 exit status
I tried to understand it and fixed but I did not resolve.
I also represented the code here.
#include <math.h>
#include "mex.h"
/* See lbfgs.m for details! */
/* This function may not exit gracefully on bad input! */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
/* Variable Declarations */
double *s, *y, *g, *H, *d, *ro, *alpha, *beta, *q, *r;
int nVars,nSteps,lhs_dims[2];
double temp;
int i,j;
/* Get Input Pointers */
g = mxGetDoubles(prhs[0]);
s = mxGetDoubles(prhs[1]);
y = mxGetDoubles(prhs[2]);
H = mxGetDoubles(prhs[3]);
/* Compute number of variables (p), rank of update (d) */
nVars = mxGetDimensions(prhs[1])[0];
nSteps = mxGetDimensions(prhs[1])[1];
/* Allocated Memory for Function Variables */
ro = mxCalloc(nSteps,sizeof(double));
alpha = mxCalloc(nSteps,sizeof(double));
beta = mxCalloc(nSteps,sizeof(double));
q = mxCalloc(nVars*(nSteps+1),sizeof(double));
r = mxCalloc(nVars*(nSteps+1),sizeof(double));
/* Set-up Output Vector */
lhs_dims[0] = nVars;
lhs_dims[1] = 1;
plhs[0] = mxCreateNumericArray(2,lhs_dims,mxDOUBLE_CLASS,mxREAL);
d = mxGetDoubles(plhs[0]);
/* ro = 1/(y(:,i)'*s(:,i)) */
for(i=0;i<nSteps;i++)
{
temp = 0;
for(j=0;j<nVars;j++)
{
temp += y[j+nVars*i]*s[j+nVars*i];
}
ro[i] = 1/temp;
}
/* q(:,k+1) = g */
for(i=0;i<nVars;i++)
{
q[i+nVars*nSteps] = g[i];
}
for(i=nSteps-1;i>=0;i--)
{
/* alpha(i) = ro(i)*s(:,i)'*q(:,i+1) */
alpha[i] = 0;
for(j=0;j<nVars;j++)
{
alpha[i] += s[j+nVars*i]*q[j+nVars*(i+1)];
}
alpha[i] *= ro[i];
/* q(:,i) = q(:,i+1)-alpha(i)*y(:,i) */
for(j=0;j<nVars;j++)
{
q[j+nVars*i]=q[j+nVars*(i+1)]-alpha[i]*y[j+nVars*i];
}
}
/* r(:,1) = q(:,1) */
for(i=0;i<nVars;i++)
{
r[i] = H[0]*q[i];
}
for(i=0;i<nSteps;i++)
{
/* beta(i) = ro(i)*y(:,i)'*r(:,i) */
beta[i] = 0;
for(j=0;j<nVars;j++)
{
beta[i] += y[j+nVars*i]*r[j+nVars*i];
}
beta[i] *= ro[i];
/* r(:,i+1) = r(:,i) + s(:,i)*(alpha(i)-beta(i)) */
for(j=0;j<nVars;j++)
{
r[j+nVars*(i+1)]=r[j+nVars*i]+s[j+nVars*i]*(alpha[i]-beta[i]);
}
}
/* d = r(:,k+1) */
for(i=0;i<nVars;i++)
{
d[i]=r[i+nVars*nSteps];
}
/* Free Memory */
mxFree(ro);
mxFree(alpha);
mxFree(beta);
mxFree(q);
mxFree(r);
}
I have been using Matlab 2019b and mexw64.
Anybody can help me. Thank you so much.
Thu Nguyen
9 Comments
Walter Roberson
on 1 Dec 2019
Your toolchain appears to be getting gcc libraries from C:/HEN_HOUSE/gnu/lib but what you have installed there is for 32 bit i386.
thu nguyen
on 2 Dec 2019
Walter Roberson
on 2 Dec 2019
mex -v -setup C
and choose an appropriate compiler. As you are using MS Windows, MinGW is supported but gcc directly is not supported.
thu nguyen
on 2 Dec 2019
Walter Roberson
on 2 Dec 2019
Check your environment variables; they are possibly pointing you to an incorrect gcc installation.
thu nguyen
on 2 Dec 2019
Bramcal
on 20 Apr 2020
have you been able to solve the problem?
thu nguyen
on 20 Apr 2020
Rahul Pathak
on 8 May 2020
I am also facing the similar type of problem and I am working on 2019b ver.
Kindly help me.
Answers (1)
James Tursa
on 25 Apr 2020
This code is incorrect:
int nVars,nSteps,lhs_dims[2];
:
plhs[0] = mxCreateNumericArray(2,lhs_dims,mxDOUBLE_CLASS,mxREAL);
The int type is almost certainly a 32-bit integer on your system, but for 64-bit compiles that dimension array needs to be mwSize (which probably converts to a 64-bit size_t integer type). So a mismatch. The code needs to be this:
mwSize lhs_dims[2];
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!