load C++ shared library into Matlab, files not found

Hello everybody,
i tried to load a C++ shared library called G+Smo into MATLAB 2016b on Linux. After compiling of G+Smo i got a shared object file "gismo.so" but no header file. After searching i found a header file "gismo.h". In this file you see a lot of "include"-commands:
#include <gsCore/gsForwardDeclarations.h>
#include <gsCore/gsExport.h>
#include <gsCore/gsLinearAlgebra.h>
#include <gsCore/gsFunctionSet.h>
...
I also have all of these other header-files in different folders as you can see above. So I tried to load this library with
loadlibrary('libgismo.so','gismo.h','includepath','/home/chu/Downloads/gismo-stable/src')
But it was not successful and i got the following error massage:
In file included from
/home/chu/Downloads/gismo-stable/src/gismo.h:44:0:
/home/chu/Downloads/gismo-stable/src/gsCore/gsForwardDeclarations.h:17:18:
fatal error: vector: No such file or directory
#include <vector>
^
compilation terminated.
gsForwardDeclarations.h includes the standard vector classes of C++ like
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
#include <limits>
But i couldn't find this file in my source folder. How can i put all the standard classes of C++ into MATLAB? Many thanks in advance for your help!

Respuestas (1)

Guillaume
Guillaume el 21 de Oct. de 2016

0 votos

" loadlibrary(libname,hfile) loads functions from shared library, libname, defined in header file, hfile, into MATLAB®. The loadlibrary function only supports calling functions that are callable from C and header files that can be parsed by a C compiler. Functions written in C++ must be declared as extern "C".
A C++ header file cannot be parsed by a C compiler.
Now you may or may not be able to solve this depending on the library. If the exported functions have C signature (they are declared as extern "C"), then the C++ headers are not necessary for talking to the library so you could create a custom header file with just the functions signatures.
If the exported functions actually have C++ signature (e.g. they use std::vector as input or output) then there is no way you can use this library with loadlibrary. Note that this is not a limitation of matlab but of C++.

5 comentarios

skyforcen73
skyforcen73 el 21 de Oct. de 2016
is there another way to load this c++ library without using loadlibrary? I have heard about MEX-files, but i don't want to create a MEX-file for every function of this library, which contains a lot of classes and functions
Guillaume
Guillaume el 21 de Oct. de 2016
Having looked at the library it does appear to be a pure C++ library.
To use it in matlab, you're either going to have to create a C interface around it (you can then use loadlibrary) or create a mex interface for it . Both are just as much work and not a trivial task unless the amount of interaction between matlab and the library is limited.
skyforcen73
skyforcen73 el 25 de Oct. de 2016
Editada: skyforcen73 el 25 de Oct. de 2016
it seems very difficult for me to create C Wrappers around the C++ code as you already mentioned. I think there is no simple way to use the library in Matlab.
I decided to switch from Linux to Windows and can create a Visual Studio Project to use the library. So is there a possibility to transfer data from Visual Studio Express 2010 into Matlab directly?
Guillaume
Guillaume el 25 de Oct. de 2016
Strange that you went with the old free but feature limited express version when the more feature complete Community version of the latest Visual Studio is also free.
Visual Studio is just a compiler / editor (a very good one), it cannot transfer data to anything, only compile code that you write. You're still stuck with having to write a wrapper of sort, be it making a dll or a mex file. Now that you're on Windows, you also have the option of making that wrapper a .Net assembly which you'd write in your favorite .Net language (VB, C#, C++, F# even). A .Net wrapper may be the easiest as it's closer to C++ but it'll still require some work.
Hi, how can I add an wrapper for c++ headers?

Iniciar sesión para comentar.

Categorías

Productos

Preguntada:

el 21 de Oct. de 2016

Comentada:

el 18 de Jun. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by