Calling Matlab function from Visual C++ application.
Show older comments
Hi. It's my first experience with Matlab and I have some problem I hope you help me to solve. I try to call some simple function written in Matlab from my C++ application. Here is function's code:
function num = MatlabAnalysesCalculateFunc
num = 7;
When I compiled this function using: mcc -W cpplib:MyMatlab -T link:lib -v MatlabAnalysesCalculateFunc
I added to my Visual Studio project( C/C++ -> Additional Include directories )path to "C:\Program Files\MATLAB\R2011a\exter\include" and of course files MyMatlab.lib, MyMatlab.h, MyMatlab.dll to project.
Now I try to call a function from C++:
mclInitializeApplication(NULL,0);
MyMatlabInitialize();
mwArray arr;
MatlabAnalysesCalculateFunc(1, arr);
MyMatlabTerminate();
mclTerminateApplication();
First problem is, that compiler gives me an error that: mclInitializeApplication_proxy': identifier not found (I did a little research and found out that there is a define mclInitializeApplication->mclInitializeApplication_proxy) If I delete this line, everything compiles fine, but linker gives me errors(and these errors are because of line mwArray arr;):
1>MatlabAnalysis.obj : error LNK2019: unresolved external symbol _mclGetEmptyArray referenced in function "public: __thiscall mwArray::mwArray(void)" (??0mwArray@@QAE@XZ)
1>MatlabAnalysis.obj : error LNK2019: unresolved external symbol _mclcppGetLastError referenced in function "public: static void __cdecl mwException::raise_error(void)" (?raise_error@mwException@@SAXXZ)
1>MatlabAnalysis.obj : error LNK2019: unresolved external symbol _mclcppCreateError referenced in function "public: __thiscall mwException::mwException(void)" (??0mwException@@QAE@XZ)
1>MatlabAnalysis.obj : error LNK2019: unresolved external symbol _ref_count_obj_addref referenced in function "public: __thiscall mwException::mwException(class mwException const &)" (??0mwException@@QAE@ABV0@@Z)
1>MatlabAnalysis.obj : error LNK2019: unresolved external symbol _ref_count_obj_release referenced in function "public: virtual __thiscall mwException::~mwException(void)" (??1mwException@@UAE@XZ)
1>MatlabAnalysis.obj : error LNK2019: unresolved external symbol _error_info_get_message referenced in function "public: virtual char const * __thiscall mwException::what(void)const " (?what@mwException@@UBEPBDXZ)
If I delete line mwArray arr; - it links Ok. Any ideas what is wrong? Any help would be highly appreciated!
Answers (3)
Kaustubha Govind
on 5 Aug 2011
0 votes
I would recommend that you first try the C Shared Library Target example from the documentation so that you become familiar with the build/deployment process. This will help you identify any steps that you are missing in your workflow.
Mohammad Alizadeh
on 17 Jan 2012
0 votes
Did you add dependency for mclmcrrt.lib? This one is necessary.
Mohammad Alizadeh
on 17 Jan 2012
0 votes
Kaustubha, I checked the Cshared library target and it works in Matlab command window (mcc , mbuild) and generated application runs successfully. However when I make a VC ( .c) project in Visual studio and generate the application, it comes up with Access violation error, reading from location and terminates and VS opens gs_support.cpp file and marks the breakpoint line: cookie = systime.ft_struct.dwLowDateTime;
I tried disabling Buffer Security Check in code generation settings but it didn't help.
Anybody able to help resolve the issue? Mohammad
2 Comments
Kaustubha Govind
on 19 Jan 2012
Seems to me like more of a Visual Studio issue than a MATLAB Compiler issue.
Mohammad Alizadeh
on 22 Jan 2012
The problem was using wrong c compiler, We should choose Visual studio's C compiler. To do this enter following command in Matlab:
mbuild -setup
and from the list of compilers select the appropriate number.
If you generate the exe file again, it should be fine.
Categories
Find more on Deploy to C++ Applications Using mwArray API (C++03) 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!