Mohammad Reza  Ramezani
Mohammad Reza Ramezani

Reputation: 772

loadlibrary and C++ header files

I wrote some functions and create dll by C++ codes & used some of the C++ header files. But I found loadlibrary only supports C header files and I get this error:

Error using loadlibrary (line 419)
Failed to preprocess the input file.
Output from preprocessor is:LargeBaseConvertorClass.h
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\eh.h(26) : fatal error    C1189: #error :  "eh.h is only for
C++!"

I dont want to change my codes and I dont want to use mex functions.

How can I use my C++ dll in matlab? (I need a lot)

Thanks.

Ya Ali.

Upvotes: 2

Views: 2386

Answers (2)

IdeaHat
IdeaHat

Reputation: 7881

I've done two things to handle this before.

The first is to write a C wrapper around the C++ code.

//foo_c_wrapper.h
#ifndef FOO_C_WRAPPER_H
#define FOO_C_WRAPPER_H

#ifdef __cplusplus
extern "C" {
#endif
typedef void* FOO_HANDLE;//can use a predeclared pointer type instead
FOO_HANDLE init_foo(int a);
void bar(FOO_HANDLE handle);
void destroy_foo(FOO_HANDLE** handle);
//ect
#endif

//foo.hpp
#ifndef FOO_HPP
#define FOO_HPP
class Foo {public: Foo(int); ~Foo(); void bar();}
#ifdef __cplusplus
}
#endif
#endif

//foo_c_wrapper.cpp
#include "foo_c_wrapper.h"
#include "foo.hpp"
extern "C" {
FOO_HANDLE init_foo(int a) {return new Foo(a);}
void bar(FOO_HANLDE handle) {
   Foo* foo = reinterpret_cast<Foo*>(handle);
   foo->bar();
}
void destroy_foo(FOO_HANDLE** handle) {
   Foo** foo = reinterpret_cast<Foo**>(handle);
   delete *foo;
   *foo = NULL;
}
}

The other option is to go the rout of creating a custom mex file. Unfortunately that topic is way too broad to go into details here, so I'm going to count "Creating a C++ compatable Mex File" as the summary of the following link:

http://www.mathworks.com/help/matlab/matlab_external/c-mex-file-examples.html#btgcjh1-14

Upvotes: 2

jpo38
jpo38

Reputation: 21514

I did that in the past by creating a a few C interface functions to create and manipulate the C++ objects. Doing this makes it possible to easily use C++ code from Matlab without having to modify it. As long as the header is only C, Matlab does not complain if C++ objects are created in the end.

For instance, if the class you want to use from Matlab is:

class MyClass
{
public:
    double memberFunction();
};

Have a header file be (add prefix to have functions be exported):

int createObject();
double callFunction( int object );

Have the cpp file be something like:

static std::map<int,MyClass*> mymap;

int createObject()
{
    MyClass* obj = new MyClass();
    int pos = mymap.size();
    mymap[pos] = obj;
    return pos;
}

double callFunction( int obj )
{
    return mymap[obj]->memberFunction();
}

Now, you can create MyClass objects and access members from Matlab.

You'll need to pass more parameters, handle map content better (check if object exists in the map and return errors if not, delete objects from the map when done...etc), but this is the general idea.

Upvotes: 0

Related Questions