How to compile a library for linking with a mexfunction?

7 Ansichten (letzte 30 Tage)
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 Kommentar
Richard Crozier
Richard Crozier am 1 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Kaustubha Govind
Kaustubha Govind am 6 Mär. 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 Kommentar
Richard Crozier
Richard Crozier am 7 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by