3

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?

4

1 回答 1

1

感谢您考虑我的问题。我发现了问题。实际上,有两个问题: 1) MATLAB 是 64 位的,但我制作了 32 位 DLL,我不得不更改 Visual Studio 中的设置以制作 64 位 DLL。2) MATLAB 用于加载 DLL 的编译器似乎与 'extern "C"' 命令有问题。所以,我改变了这样的标题:

#ifndef DllTest_h
#define DllTest_h

#include <stdio.h>

#ifdef __cplusplus

extern "C" {

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

#ifdef __cplusplus
}
#endif


#endif

最后它奏效了。

于 2013-01-22T16:53:40.253 回答