loadlibrary and 64bit shared lib (DLL) on 64bit windows 7

18 vues (au cours des 30 derniers jours)
Greg Thom
Greg Thom le 9 Avr 2012
Hi All,
I have source of a library which compiles and loads fine on 64bit unix and 32 bit windows. Now I am trying to compile the dll on win7 64 bit with vs studio 8 (32 bit with win64 option) but I keep getting and error when using loadlibrary in matlab (2011b) for the dll which I have managed to compile.
The error is simply:
Error using loadlibrary (line 421)
Building libTestDLL_thunk_pcwin64 failed.
After this error, I get a bunch of errors on my h file like:
error C2134 syntax error : missing ')' before '*'
So my h file is wrong ?
I am not sure what is going on here. I have read that the 64bit matlab loadlibrary expects special treatment of file extension c vs c++ ? What is the right way to compile my libary so that it doesn't give problems with loadlibrary ?
This is my main h file for the library:
#ifndef TestDLL_h
#define TestDLL_h
/* This is from shrhelp.h */
#ifndef SHRHELP
#define SHRHELP
#ifdef _WIN32
#ifdef EXPORT_FCNS
#define EXPORTED_FUNCTION __declspec(dllexport)
#else
#define EXPORTED_FUNCTION __declspec(dllimport)
#endif
#else
#define EXPORTED_FUNCTION
#endif
#endif
/* end shrhelp.h section */
#ifdef __cplusplus
extern "C"
{
#endif
EXPORTED_FUNCTION void testLoadData( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] );
#ifdef __cplusplus
}
#endif
#endif /* TestDLL_h */
This is one of my cpp files:
#include "mex.h"
#include "matrix.h"
#define EXPORT_FCNS
#include "TestDLL.h"
#include "StaticControl.h"
#include "volume.h"
#include <stdio.h>
#include <stdlib.h>
StaticControl g_Control;
#ifdef __cplusplus
extern "C" {
#endif
EXPORTED_FUNCTION void testLoadData( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] )
{
if (nlhs > 0)
mexErrMsgTxt("Too many output arguments.");
if (nrhs != 5)
mexErrMsgTxt("Format: ID, xVals, yVals, zVals, volumeData");
if (!mxIsChar(prhs[0]))
mexErrMsgTxt("ID should be a string.");
if (!mxIsDouble(prhs[1]))
mexErrMsgTxt("xVals should be an array of doubles.");
if (!mxIsDouble(prhs[2]))
mexErrMsgTxt("yVals should be an array of doubles.");
if (!mxIsDouble(prhs[3]))
mexErrMsgTxt("zVals should be an array of doubles.");
if (!mxIsDouble(prhs[4]))
mexErrMsgTxt("volumeData should be an array of doubles.");
return;
}
#ifdef __cplusplus
}
#endif
If there is a recommended way of doing this I will be glad to hear
Thank you
GT
  2 commentaires
Walter Roberson
Walter Roberson le 9 Avr 2012
Greg, in the list of errors, is there perhaps one that says something about not being able to open a file that ends with .h such as windows.h ?
Greg Thom
Greg Thom le 10 Avr 2012
Thanks for your comment Walter, but I am not seeing any unable to open file error: In fact here is a complete error I see.
>> loadlibrary('libTestDLL','TestDLL.h');
Error using loadlibrary (line 421)
Building libTestDLL_thunk_pcwin64 failed. Compiler output is:
cl -I"C:\Program Files\MATLAB\R2011b\extern\include" /W3
/D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0
/nologo -I"C:\Development\WORK"
-I"C:\Development\WORK" "libTestDLL_thunk_pcwin64.c"
-LD -Fe"libTestDLL_thunk_pcwin64.dll"
libTestDLL_thunk_pcwin64.c
C:\Development\WORK\TestDLL.h(26) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(26) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(26) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(26) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(26) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(26) : error C2059:
syntax error : ')'
C:\Development\WORK\TestDLL.h(27) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(27) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(27) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(27) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(27) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(27) : error C2059:
syntax error : ')'
C:\Development\WORK\TestDLL.h(29) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(29) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(29) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(29) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(29) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(29) : error C2059:
syntax error : ')'
C:\Development\WORK\TestDLL.h(30) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(30) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(30) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(30) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(30) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(30) : error C2059:
syntax error : ')'
C:\Development\WORK\TestDLL.h(31) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(31) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(31) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(31) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(31) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(31) : error C2059:
syntax error : ')'
C:\Development\WORK\TestDLL.h(33) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(33) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(33) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(33) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(33) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(33) : error C2059:
syntax error : ')'
C:\Development\WORK\TestDLL.h(34) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(34) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(34) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(34) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(34) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(34) : error C2059:
syntax error : ')'
C:\Development\WORK\TestDLL.h(36) : error C2143:
syntax error : missing ')' before '*'
C:\Development\WORK\TestDLL.h(36) : error C2081:
'mxArray' : name in formal parameter list illegal
C:\Development\WORK\TestDLL.h(36) : error C2143:
syntax error : missing '{' before '*'
C:\Development\WORK\TestDLL.h(36) : error C2133:
'plhs' : unknown size
C:\Development\WORK\TestDLL.h(36) : error C2059:
syntax error : 'type'
C:\Development\WORK\TestDLL.h(36) : error C2059:
syntax error : ')'

Connectez-vous pour commenter.

Réponse acceptée

Geoff
Geoff le 10 Avr 2012
It definitely doesn't like mxArray, so I'd say that you are including files in the wrong order. In the source files that show the error, are you including TestDLL.h before you include mex.h and matrix.h? That would do it.
Either change the order in the relevant files, or at least include matrix.h from within TestDLL.h.
Also, I'd recommend you use:
#include <mex.h>
#include <matrix.h>
Instead of
#include "mex.h"
#include "matrix.h"
Just on the off-chance that the compiler is picking up the wrong file. In particular, it's not too unlikely that a project might contain a file called "matrix.h".
  1 commentaire
Greg Thom
Greg Thom le 10 Avr 2012
Thank you. I moved the include lines to TestDLL.h and the thunk file was built no problems.

Connectez-vous pour commenter.

Plus de réponses (1)

Philip Borghesani
Philip Borghesani le 10 Avr 2012
When building the thunk file your header file must be able to compile standalone. Your header file needs mex.h as a prerequisite so it should have the line #include &ltmex.h&gt near the beginning of the file.
The key compile error is the second one: The compiler does not know what an mxArray is.
The standard header parser in loadlibrary requires standalone headers too but it is a bit more forgiving about missing definitions of standard data types.
The best way to test a header file is to create and compile the the file main.c with:
#include "myHeader.h"
int main(int, char **)
{
}
Creating header files that meet this requirement is a common best practice in modern c and c++ programming.
  1 commentaire
Greg Thom
Greg Thom le 10 Avr 2012
I was able to compile this dll in VS2008 so I guess all was correct there.

Connectez-vous pour commenter.

Catégories

En savoir plus sur C Shared Library Integration dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by