Pouya
Pouya

Reputation: 1959

Unable to load a library in MATLAB

I have written a very very simple program in Visual C++ 2008 SP1. It just adds up two numbers. The DLLTest.cpp is:

#include "DllTest.h"

    __declspec(dllexport) double Add(double a, double b)
    {
    return( a + b );
}

And DllTest.h is:

#ifndef _DLL_TEST_H_
#define _DLL_TEST_H_
#endif

__declspec(dllexport) double Add( double, double);

I build the DLL using Visual C++ 2008. When I try to load the library using loadlibrary, I get the following error:

??? Error using ==> loadlibrary at 422 Building DllTest_thunk_pcwin64 failed. Compiler output is: DllTest_thunk_pcwin64.c C:\Users\Admin\Desktop\DllTest.h(5) : error C2054: expected '(' to follow 'EXPORTED_FUNCTION' C:\Users\Admin\Desktop\DllTest.h(5) : error C2085: 'Add' : not in formal parameter list DllTest_thunk_pcwin64.c(40) : error C2085: 'int8' : not in formal parameter list DllTest_thunk_pcwin64.c(41) : error C2085: 'uint8' : not in 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 formal parameter list DllTest_thunk_pcwin64.c(44) : error C2085: 'int32' : not in formal parameter list DllTest_thunk_pcwin64.c(45) : error C2085: 'uint32' : not in formal parameter list DllTest_thunk_pcwin64.c(46) : error C2085: 'int64' : not in formal parameter list DllTest_thunk_pcwin64.c(47) : error C2085: 'uint64' : not in formal 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 formal parameter list DllTest_thunk_pcwin64.c(51) : error C2082: redefinition of 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 want just to load a simple program, written in Visual C++, in MATLAB. How can I fix this problem?

Upvotes: 3

Views: 945

Answers (1)

Pouya
Pouya

Reputation: 1959

Thanks for considering my question. I found the problem. Actually, there were two problems: 1) The MATLAB is 64 bit but I made 32-bit DLL and I had to change the settings in Visual Studio to make 64-bit DLL. 2) It seems the compiler that MATLAB uses for loading the DLL, has problem with 'extern "C"' command. So, I changed the header like this:

#ifndef DllTest_h
#define DllTest_h

#include <stdio.h>

#ifdef __cplusplus

extern "C" {

#endif
__declspec(dllexport) double Add( double, double);

#ifdef __cplusplus
}
#endif


#endif

And Finally it worked.

Upvotes: 1

Related Questions