How to compile a library for linking with a mexfunction?

8 visualizaciones (últimos 30 días)
I have a C++ program which I am constructing a mex interface for. This program's source code files are contained in a single folder. In this folder I also have a subfolder which contains the source code files of a library which must be compiled and linked with the main program.
Is there a way of telling the mex compiler script to compile and link this library with my code? I understand how to link an existing library file, but not how I could also compile this library first.
Ideally I would like this process to be transparent to a user (so they can simply call a setup mfile script or function), and not requiring them to know how to build a library file outside of Matlab, but also not requiring me to provide the library file for their platform. For similar reasons I would like any solution to work with an unmodified Matlab installation.
  1 comentario
Richard Crozier
Richard Crozier el 1 de Mzo. de 2012
Just to be clear, I can compile and link the program and library outside of Matlab fine, it is just performing this from a matlab script that I'm interested in.

Iniciar sesión para comentar.

Respuesta aceptada

Kaustubha Govind
Kaustubha Govind el 6 de Mzo. de 2012
You can call your library build make commands from MATLAB using the system command. Of course this might fail if your user does not have the right compiler installed. One way to make this fool-proof is to first ask the user to setup the MEX compiler (using "mex -setup") with a supported C++ compiler. Once this is done, you can find the path to the installed compiler using:
cppCompilerConfig = mex.getCompilerConfigurations('C++');
pathToCompilerDirectory = cppCompilerConfig.Location;
You can then locate g++ or cl.exe (or whatever compiler command you use) relative to this path and run the SYSTEM command by specifying the full path to the compiler.
Once the library is ready, you simply need to append the library name with the mex -l option (specify the path containing the library using the -L option if it is not present in the current directory). For example:
>> mex myMexFunction.c -lmyLibFile
  1 comentario
Richard Crozier
Richard Crozier el 7 de Mzo. de 2012
This is the approach I'm going to take, and if I can make it generic enough I will post a function to do this on the file exchange.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by