Borrar filtros
Borrar filtros

MATLAB dll in Visual C++

12 visualizaciones (últimos 30 días)
Tina
Tina el 16 de Jun. de 2017
Editada: Kojiro Saito el 19 de Jun. de 2017
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

Respuesta aceptada

Kojiro Saito
Kojiro Saito el 17 de Jun. de 2017
It seems the linker has a problem. Could you confirm mclmcrrt.lib was added to "Additional Dependencies" under the Linker ->Input tab?
  2 comentarios
Tina
Tina el 19 de Jun. de 2017
Editada: Tina el 19 de Jun. de 2017
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 el 19 de Jun. de 2017
Editada: Kojiro Saito el 19 de Jun. de 2017
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre C Shared Library Integration en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by