connecting matlab to c++ language dll

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!

Answers (2)

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 Comments

Mahnaz
Mahnaz on 6 Aug 2012
Edited: Mahnaz on 6 Aug 2012
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?
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)

Sign in to comment.

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 Comments

i changed my header file:
#ifndef _T1_HEADER_H_
#define _T1_HEADER_H_
extern int Add( int a, int b );
extern void Function( void );
#endif
my source file is:
#include <iostream>
#include "T1Header.h"
extern int Add( int a, int b )
{
return( a + b );
}
extern void Function( void )
{
std::cout << "DLL Called!" << std::endl;
}
i built the project
when i want to use loadlibrary function:
>> loadlibrary('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:21 EOF inside comment
> In loadlibrary at 351
Warning: The function 'Add' was not found in the library
> In loadlibrary at 435
Warning: The function 'Function' was not found in the library
> In loadlibrary at 435
and for calllib function:
>> calllib('t1', 'Add', 2,3)
??? Error using ==> calllib
Method was not found.
i think i should insall visual C++ 2008
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
I have the exactly same error with Visual Studio 2008

Sign in to comment.

Categories

Tags

Asked:

on 6 Aug 2012

Commented:

on 7 Feb 2014

Community Treasure Hunt

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

Start Hunting!