MATLAB dll in Visual C++

12 次查看(过去 30 天)
Tina
Tina 2017-6-16
I am trying to call a very simple MATLAB function from my Visual C++ code, but despite the endless number of tutorials on how to do this (e.g. this one ), I keep getting the same errors.
I use the Library Compiler in MATLAB R2016b to generate TestingDLLMain.dll, and Visual Studio 2012 (11.0). I downloaded the MCR for MATLAB R2016b.
This is my MATLAB function:
function [number] = TestingDLL(n)
number = n+1;
end
And C++:
#include "stdafx.h"
#include <iostream>
#include "TestingDLLMain.h"
int _tmain(int argc, _TCHAR* argv[])
{
// initialize MCR and lib
if (!mclInitializeApplication(NULL,0)) {
std::cerr << "could not initialize the application" << std::endl;
return -1;
}
if(!TestingDLLMainInitialize()) {
std::cerr << "Could not initialize the library" << std::endl;
return -1;
}
try {
// create input
double a[] = {1.0, 2.0, 3.0, 4.0};
mwArray in1(2, 2, mxDOUBLE_CLASS, mxREAL);
in1.SetData(a, 4);
// call function
mwArray out;
TestingDLL(1, out, in1);
// show result
std::cout << "in1 + 1 = " << out << std::endl;
}
catch (const mwException& e) {
std::cerr << e.what() << std::endl;
return -2;
}
return 0;
}
The error:
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _mclInitializeApplication_860_proxy referenced in function _wmain
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _ref_count_obj_addref_proxy referenced in function "public: __thiscall mwException::mwException(class mwException const &)" (??0mwException@@QAE@ABV0@@Z)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _ref_count_obj_release_proxy referenced in function "public: virtual __thiscall mw_auto_ptr_t<class char_buffer>::~mw_auto_ptr_t<class char_buffer>(void)" (??1?$mw_auto_ptr_t@Vchar_buffer@@@@UAE@XZ)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _char_buffer_get_buffer_proxy referenced in function "public: __thiscall mwString::operator char const *(void)const " (??BmwString@@QBEPBDXZ)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _array_ref_to_string_proxy referenced in function "public: class mwString __thiscall mwArray::ToString(void)const " (?ToString@mwArray@@QBE?AVmwString@@XZ)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _array_ref_set_numeric_mxDouble_proxy referenced in function "public: void __thiscall mwArray::SetData(double *,unsigned int)" (?SetData@mwArray@@QAEXPANI@Z)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _error_info_get_message_proxy referenced in function "public: virtual char const * __thiscall mwException::what(void)const " (?what@mwException@@UBEPBDXZ)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _mclcppGetLastError_proxy referenced in function "public: static void __cdecl mwException::raise_error(void)" (?raise_error@mwException@@SAXXZ)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _mclcppCreateError_proxy referenced in function "public: __thiscall mwException::mwException(class error_info *,bool)" (??0mwException@@QAE@PAVerror_info@@_N@Z)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _mclGetEmptyArray_proxy referenced in function "public: __thiscall mwArray::mwArray(void)" (??0mwArray@@QAE@XZ)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _mclGetMatrix_proxy referenced in function "public: __thiscall mwArray::mwArray(unsigned int,unsigned int,enum mxClassID,enum mxComplexity)" (??0mwArray@@QAE@IIW4mxClassID@@W4mxComplexity@@@Z)
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol _TestingDLLMainInitialize referenced in function _wmain
1>ConsoleApplication3.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl TestingDLL(int,class mwArray &,class mwArray const &)" (__imp_?TestingDLL@@YAXHAAVmwArray@@ABV1@@Z) referenced in function _wmain
1>C:\Users\vrt9ti\Documents\Visual Studio 2012\Projects\ConsoleApplication3\Debug\ConsoleApplication3.exe : fatal error LNK1120: 13 unresolved externals

采纳的回答

Kojiro Saito
Kojiro Saito 2017-6-17
It seems the linker has a problem. Could you confirm mclmcrrt.lib was added to "Additional Dependencies" under the Linker ->Input tab?
  2 个评论
Tina
Tina 2017-6-19
编辑:Tina 2017-6-19
It seems so, yes. Is it possible that things don't work due to incompatibility issues? Or did I add the library in a wrong way, if that is even possible? I just typed "mclmcrrt.lib" into the field. In a previous project, I added another library in the same way and that worked fine.
Kojiro Saito
Kojiro Saito 2017-6-19
编辑:Kojiro Saito 2017-6-19
Just "mclmcrrt.lib" works. In your previous project, did you define Solution Platforms as "Win32"? It should be x64.
I've just tested Visual Studio 2012 and MATLAB / MATLAB Runtime R2016b. Since MATLAB R2016b dll only supports 64bit OS, you should change target platform from Win32 or x86 to x64.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 C Shared Library Integration 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by