connecting matlab to c++ language dll

조회 수: 6 (최근 30일)
Mahnaz
Mahnaz 2012년 8월 6일
댓글: Guilherme 2014년 2월 7일
hello
i'm trying to use clllib function in matlab i put .dll file and .h file in the same directory as the .m MATLAB file being developed and after that i try to use loadlibrary function bui it have two warnings:
l oadlibrary('t1.dll','T1Header.h') Warning: Message from C preprocessor: lcc preprocessor error: C:\Users\MAHNAZ\Documents\MATLAB\T1Header.h:1 Could not find include file iostream lcc preprocessor warning: C:\Users\MAHNAZ\Documents\MATLAB\T1Header.h:18 No newline at end of file
when i want to use calllib function like this:
calllib('t1', 'Add', 2,3)
matlab give me an error:
??? Error using ==> calllib
Method was not found.
my header file is:
#include <iostream>
#ifndef _T1_HEADER_H_
#define _T1_HEADER_H_
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
extern "C"
{
__declspec(dllexport) int Add( int a, int b );
__declspec(dllexport) void Function( void );
}
#endif
i use visual c++ 2010
Any suggestions on what is wrong, what I could do to fix this error, or what else I could try to call this .dll from within MATLAB?
thanks!

답변 (2개)

Titus Edelhofer
Titus Edelhofer 2012년 8월 6일
Hi,
did you tell MATLAB to use visual studio as well? Looks as if it uses LCC, the C compiler that comes bundled with MATLAB. Do a
mex -setup
and choose your visual studio, that should help.
Titus
  댓글 수: 2
Mahnaz
Mahnaz 2012년 8월 6일
편집: Mahnaz 2012년 8월 6일
thanks
i use Matlab 7.6.0(R2008a) and i have Visual Studio 2010 installed. However, MATLAB doesn't find the compiler. when i want to do this:
>> mex -setup Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? n
Select a compiler:
[1] Intel C++ 9.1 (with Microsoft Visual C++ 2005 linker)
[2] Intel Visual Fortran 10.1 (with Microsoft Visual C++ 2005 linker)
[3] Intel Visual Fortran 9.1 (with Microsoft Visual C++ 2005 linker)
[4] Lcc-win32 C 2.4.1
[5] Microsoft Visual C++ 6.0
[6] Microsoft Visual C++ .NET 2003
[7] Microsoft Visual C++ 2005
[8] Microsoft Visual C++ 2005 Express Edition
[9] Microsoft Visual C++ 2008
[10] Open WATCOM C++
[11] Open WATCOM C++ 1.3
[0] None
Compiler: 9
The default location for Microsoft Visual C++ 2008 compilers is C:\Program Files\Microsoft Visual Studio 9.0, but that directory does not exist on this machine.
Use C:\Program Files\Microsoft Visual Studio 9.0 anyway [y]/n? y
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 Location: C:\Program Files\Microsoft Visual Studio 9.0
what should i do? install visual C++ 2008?
Kaustubha Govind
Kaustubha Govind 2012년 8월 6일
This is the list of supported compilers for R2008a - Visual Studio 2010 is not on the list, which is why "mex -setup" doesn't find it. I would recommend installing one of the supported compilers and answer "y" to "Would you like mex to locate installed compilers [y]/n?" - because if MATLAB is unable to find your compiler automatically, there is usually something wrong with your compiler setup.
(Note: You cannot use the LCC compiler to preprocess a C++ header, because it is a C-only compiler, so Titus' solution of converting your header to C-style should work with LCC)

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


Titus Edelhofer
Titus Edelhofer 2012년 8월 6일
Hmm, I just took another look on your question: probably the easiest thing to do is to write a second header file just containing the declarations:
extern int Add( int a, int b );
extern void Function( void );
and use this file to call loadlibrary.
Titus
  댓글 수: 3
Titus Edelhofer
Titus Edelhofer 2012년 8월 7일
편집: Walter Roberson 2012년 8월 17일
iostream is C++, lcc is C, so you need to
- remove the "include iostream" line
- add #include <stdio.h>
- replace the cout line by
printf("DLL Called.\n");
Titus
Guilherme
Guilherme 2014년 2월 7일
I have the exactly same error with Visual Studio 2008

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

카테고리

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