connecting matlab to c++ language dll

7 ビュー (過去 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 Center および File ExchangeC Shared Library Integration についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by