mexFuncion.obj : error LNK2019: unresolved external symbol _mxGetPr referenced in function _mexFunction

18 views (last 30 days)
Hi, I am learning how to interface matlab with c++, I am following a simple example, "timestwo.c", in matroot/extern/examples/refbook/,
#include "mex.h" #pragma comment(lib,"libmx.lib")
#pragma comment(lib, "libmat.lib")
#pragma comment(lib, "libmex.lib")
void timestwo(double y[], double x[]) { y[0] = 2.0*x[0]; }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *x,*y; mwSize mrows,ncols;
/* Check for proper number of arguments. */
if(nrhs!=1) {
mexErrMsgTxt("One input required.");
} else if(nlhs>1) {
mexErrMsgTxt("Too many output arguments.");
}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
!(mrows==1 && ncols==1) ) {
mexErrMsgTxt("Input must be a noncomplex scalar double.");
}
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
y = mxGetPr(plhs[0]);
/* Call the timestwo subroutine. */
timestwo(y,x);
}
i was able to compile it in Microsoft visual C++ 2010 successfully, but when I build it, i get linking errors as follows,
mexFuncion.obj : error LNK2019: unresolved external symbol _mxGetPr referenced in function _mexFunction mexFuncion.obj : error LNK2019: unresolved external symbol _mxCreateDoubleMatrix_730 referenced in function _mexFunction mexFuncion.obj : error LNK2019: unresolved external symbol _mxIsComplex referenced in function _mexFunction mexFuncion.obj : error LNK2019: unresolved external symbol _mxIsDouble referenced in function _mexFunction mexFuncion.obj : error LNK2019: unresolved external symbol _mxGetN referenced in function _mexFunction mexFuncion.obj : error LNK2019: unresolved external symbol _mxGetM referenced in function _mexFunction mexFuncion.obj : error LNK2019: unresolved external symbol _mexErrMsgTxt referenced in function _mexFunction
I think linkers cannot found library such as "libmx.lib" libmat.lib" libmex.lib", but i set the path mathroot\extern\lib\win64\microsoft under the the project->property->linker->general->additional library directories. In addition, i have a mexFunction.def as follows: LIBRARY “My_project" EXPORTS mexFunction
where else could be missing for those errors?

Answers (1)

Friedrich
Friedrich on 18 Jul 2011
Hi,
you have to set some more. I created a MSVC timestwo.c example and uploaded it here:
  2 Comments
Wei
Wei on 18 Jul 2011
Edited: Walter Roberson on 9 Aug 2020
Hi, Thank you for your reply, I download your project and try to run on my desktop, my visual c++ 2010 says your version is old, it needs to be auto converted to a newest version. So I did the auto conversion. and after change additional library path for compiler, I was able to compile it but keep get the same error as follows,
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(990,5): warning MSB8012: TargetPath(C:\Users\PC\Desktop\mcvs mex setup\mex_test\Release\mex_test.dll) does not match the Linker's OutputFile property value (C:\Users\PC\Desktop\mcvs mex setup\mex_test\Release\mex_test.mexw32). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppBuild.targets(991,5): warning MSB8012: TargetExt(.dll) does not match the Linker's OutputFile property value (.mexw32). This may cause your project to build incorrectly. To correct this, please make sure that $(OutDir), $(TargetName) and $(TargetExt) property values match the value specified in %(Link.OutputFile).
Creating library C:\Users\PC\Desktop\mcvs mex setup\mex_test\Release\mex_test.lib and object C:\Users\PC\Desktop\mcvs mex setup\mex_test\Release\mex_test.exp
mex_test.exp : warning LNK4070: /OUT:mex_test.dll directive in .EXP differs from output filename 'C:\Users\PC\Desktop\mcvs mex setup\mex_test\Release\mex_test.mexw32'; ignoring directive
timestwo.obj : error LNK2001: unresolved external symbol _mxIsComplex
timestwo.obj : error LNK2001: unresolved external symbol _mxIsDouble
timestwo.obj : error LNK2001: unresolved external symbol _mxGetN
timestwo.obj : error LNK2001: unresolved external symbol _mxGetPr
timestwo.obj : error LNK2001: unresolved external symbol _mexErrMsgTxt
timestwo.obj : error LNK2001: unresolved external symbol _mxGetM
timestwo.obj : error LNK2001: unresolved external symbol _mxCreateDoubleMatrix_730
C:\Users\PC\Desktop\mcvs mex setup\mex_test\Release\mex_test.mexw32 : fatal error LNK1120: 7 unresolved externals
which version of your visual studio is? Do you think it matters?
Friedrich
Friedrich on 18 Jul 2011
Hi,
I used VS 2008. You have to modify the path in the project settings so they match your system and installation of your ML.
1.) Under C/C++ ->gernal -> additional include dirs: MATLABROOT\extern\include
2.) Under Linker -> general -> additional lib dirs you have to add the libraries directory: MATLABROOT\extern\lib\win32\microsoft
3.)Under Linker -> Input -> additional dependencies you have to add:
libmx.lib libmex.lib libmat.lib

Sign in to comment.

Categories

Find more on Programming 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!