2

I am trying to build a dll with visual studio so I can use it in matlab ... I tried thausand codes with no solution !!! I am working on matlab 2013(32and64) and VS2010 ! I tried for example to write the code this way ...

        //The header

    #ifndef SIMPLEH_H
    #define SIMPLEH_H
    #ifdef  __cplusplus
    extern "C" {
    int sq(int x);  
    #endif
    #ifdef  __cplusplus
    }
    #endif
    #endif

//the Func(example)

#include "SimpleH.h"
int sq(int x)
{
return (x*x);
}

visual studio Build it and make th dll file but matlab always doesn't see the function ... What should I do /* I am really stucked :( */ Thanks in advance ...

Amro
  • 123,847
  • 25
  • 243
  • 454
user130227
  • 59
  • 2
  • 8
  • are you trying to use the DLL using [`loadlibrary`](http://www.mathworks.com/help/matlab/ref/loadlibrary.html), or are you trying to write a [MEX-function](http://www.mathworks.com/help/matlab/create-mex-files.html)? The documentation is a good place to start either way.. – Amro Aug 24 '13 at 05:31
  • Thank you for your reply... yes, I am using loadlibrary and it load it without problems but when I use calllib it say "Method was not found" so I am confusing what should I write in visual studio to make it what syntax should I use ... is my syntax above is right or ??? thank you again ... – user130227 Aug 24 '13 at 17:13
  • can you post the code you tried in MATLAB to load the DLL and call the library? It should be straightforward – Amro Aug 24 '13 at 17:21
  • oh, you might wanna do the usual dllexport/dllimport in the header file. For example see the `helper.h` file in this other answer of mine, and how it it included from both the library header file and the implementation C code: http://stackoverflow.com/a/18071239/97160 – Amro Aug 24 '13 at 17:24
  • I just open the matlab and type loadlibrary('mylibdll','SimpleH') and then I write calllib('mylibdll','sq',3) and it say "Method was not found" ... I will try to read your code and get back to you ... Big thank to you ... – user130227 Aug 24 '13 at 17:36
  • I posted an example below. You had a subtle mistake in the way you wrapped the function prototype inside #ifdef blocks. – Amro Aug 24 '13 at 17:59

1 Answers1

2

Example: Take the following files, and build a DLL in Visual Studio.

helper.h

#ifndef HELPER_H
#define HELPER_H

#ifdef _WIN32
#ifdef EXPORT_FCNS
#define EXPORTED_FUNCTION __declspec(dllexport)
#else
#define EXPORTED_FUNCTION __declspec(dllimport)
#endif
#else
#define EXPORTED_FUNCTION
#endif

#endif

simple.h

#ifndef SIMPLEH_H
#define SIMPLEH_H

#include "helper.h"

#ifdef  __cplusplus
extern "C" {
#endif

EXPORTED_FUNCTION int sq(int x);

#ifdef  __cplusplus
}
#endif

#endif

simple.cpp

#define EXPORT_FCNS
#include "helper.h"
#include "simple.h"

int sq(int x)
{
    return (x*x);
}

Copy the generated simple.dll and the header files simple.h and helper.h in the current directory. Then in MATLAB:

>> loadlibrary('./simple.dll', './simple.h')

>> libisloaded simple
ans =
     1

>> libfunctions simple -full
Functions in library simple:

int32 sq(int32)

>> calllib('simple', 'sq',3)
ans =
     9

Note: If you are running MATLAB 64-bit, you must build the DLL as such. The rule is that you cannot load a 32-bit library in a 64-bit process.

Amro
  • 123,847
  • 25
  • 243
  • 454
  • see [here](http://stackoverflow.com/q/57999/97160) for an explanation of dllimport/dllexport. [This one](http://stackoverflow.com/q/3789340/97160) describes mixing C and C++ – Amro Aug 24 '13 at 18:13