Calling Matlab function from Visual C++ application.

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!

답변 (3개)

Kaustubha Govind
Kaustubha Govind 2011년 8월 5일

0 개 추천

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
Mohammad Alizadeh 2012년 1월 17일

0 개 추천

Did you add dependency for mclmcrrt.lib? This one is necessary.
Mohammad Alizadeh
Mohammad Alizadeh 2012년 1월 17일

0 개 추천

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

Seems to me like more of a Visual Studio issue than a MATLAB Compiler issue.
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Deploy to C++ Applications Using mwArray API (C++03)에 대해 자세히 알아보기

질문:

2011년 8월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by