How to use so file in Matlab?

16 vues (au cours des 30 derniers jours)
Sungmin Jo
Sungmin Jo le 28 Déc 2017
Commenté : he zhang le 19 Nov 2019
1. Can I use so file in Windows matlab? If yes, How to use it? 2. Can I use so file in Linux matlab? If yes, How to use it?
I want to use .so file in matlab. I made so file from c++ in linux. Can I use that so file in matlab?
I want to use it in windows matlab, but if it impossible, I can use matlab in linux.

Réponses (1)

Harish Ramachandran
Harish Ramachandran le 2 Jan 2018
Hello Sungmin,
If I understand correctly, you have a .so file along with C++ code which needs to be run in MATLAB. If that is the case, you can use Mex files in order to call your C++ code (along with the shared .so library file as a dependency) from the MATLAB command line. This is independent of the OS. You need to setup a supported mex C++ compiler and write a gateway function in C++.
After completing the above steps, you can proceed to compile the C++ code with the .so file.
mex gateway.cpp source.cpp file.so
  3 commentaires
Harish Ramachandran
Harish Ramachandran le 2 Jan 2018
I can give you a general example of compiling an .obj/.so file using MEX. For your case, I do not know what the structure of the .so file is. Since you have that knowledge you could translate the information below to suit your needs. This is a very trivial example which I put together.
Assume, I have a computational subroutine to multiply two numbers (written in C). The name of the file is mult.c
#include "mex.h"
/* The computational routine */
void mult(double *x, double *y, double *z)
{
double c;
*x = (*y) * (*z);
return;
}
You can convert that into an obj file (similar to .so file using the MEX compiler)
mex -c mult.c
The MEX compiler I am using is MinGW64 which is supported by MATLAB. (refer to the link in the answer above)
In order to compute the computational code in MATLAB, you need to write a gateway function - mexFunction as shown below:
#include "mex.h"
void mult(double *, double *, double *); % Prototype for the function in the .obj file
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
#define A_IN prhs[0]
#define B_IN prhs[1]
#define B_OUT plhs[0]
double *a, *b;
double *result;
B_OUT = mxCreateDoubleMatrix(1, 1, mxREAL);
a = mxGetPr(A_IN);
b = mxGetPr(B_IN);
result = mxGetPr(B_OUT);
mult(result, a, b);
}
Please note that this is to demonstrate how to run with a dependent object file. You will need to read the documentation on MEX files in order to understand the piece of code. Check out MathWorks documentation which explains how to write MEX file. Assuming that the code is in a file named gateway.c, you can see below how the object file is linked while compiling. In a similar way, you will need to link the .so file.
mex gateway.c mult.obj
Building with 'MinGW64 Compiler (C)'.
MEX completed successfully.
y = gateway(2,3)
y =
6
Hope this helps!
he zhang
he zhang le 19 Nov 2019
Hi,
i encountered a problem on apply .so file:
firstly , i use mex command to compile mex function:
mex -v MexTest3.cpp libDemo3.so
and the information shows mex completed successfully. but then
when i use MexTest3() to apply mex function, it shows:
libDemo3.so: cannot open shared object file: No such file or directory.
it seems like .so file is not included or not in included path, so i did a test:
i create a main.cpp to apply libDemo3.so and it works.:
g++ mian.cpp -L. -lDemo3 -o main
So, something seems to be wrong with my matlab Mex. Do you have any suggestions? Thanks.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!