Unable to load library in MATLAB
In Visual C ++ 2008 SP1, I wrote a very simple program. It just adds two numbers. DLLTest.cpp:
#include "DllTest.h"
__declspec(dllexport) double Add(double a, double b)
{
return( a + b );
}
And DllTest.h:
#ifndef _DLL_TEST_H_
#define _DLL_TEST_H_
#endif
__declspec(dllexport) double Add( double, double);
I am creating a DLL using Visual C ++ 2008. When I try to load a library using loadlibrary
I get the following error:
??? Error using ==> loadlibrary in 422 Building DllTest_thunk_pcwin64 failed. Compiler output: DllTest_thunk_pcwin64.c C: \ Users \ Admin \ Desktop \ DllTest.h (5): error C2054: expected '(' follow 'EXPORTED_FUNCTION' C: \ Users \ Admin \ Desktop \ DllTest.h (5): error C2085: "Add": not in the formal parameter list DllTest_thunk_pcwin64.c (40): error C2085: 'int8': not in the official parameter list DllTest_thunk_pcwin64.c (41): error C2085: 'uint8': not in the formal parameter list DllTest_thunk_pcwin64 .c (42): error C2085: 'int16': not in formal Parameter list DllTest_thunk_pcwin64.c (43): error C2085: 'uint16': not in official parameter list DllTest_thunk_pcwin64.c (44): error C2085: 'int32' : not in the list of formal parameters DllTest_thunk_pcwin64.c (45):error C2085: 'uint32': not in formal Parameter list DllTest_thunk_pcwin64.c (46): error C2085: 'int64': not in official parameter list DllTest_thunk_pcwin64.c (47): error C2085: 'uint64': not in official parameter list DllTest_thunk_pcwin64.c (48): error C2085: 'voidPtr': not in formal Parameter list DllTest_thunk_pcwin64.c (49): error C2085: 'string': not in official parameter list DllTest_thunk_pcwin64.c (51): error C2082: overriding formal parameter "EXPORTED_FUNCTION" DllTest_thunk_pcwin64.c (51): error C2143: syntax error: missing ';' before "type" DllTest_thunk_pcwin64.c (52): error C2085: 'EXPORTED_FUNCTIONdoubledoubledoubleThunk': not in formal parameter list DllTest_thunk_pcwin64.c (52): error C2143:syntax error: missing ';' before '{'
I just want to load a simple program written in Visual C ++ into MATLAB. How can I fix this problem?
source to share
Thanks for reviewing my question. I found the problem. There were actually two problems: 1) MATLAB is 64 bit, but I made a 32 bit DLL and I had to change the settings in Visual Studio to make a 64 bit DLL. 2) It seems the compiler that MATLAB uses to load the DLL has a problem with the "extern" C "command. So, I changed the title as follows:
#ifndef DllTest_h
#define DllTest_h
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) double Add( double, double);
#ifdef __cplusplus
}
#endif
#endif
Finally, it worked.
source to share