How to compile a library for linking with a mexfunction?

3 次查看(过去 30 天)
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 个评论
Richard Crozier
Richard Crozier 2012-3-1
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.

请先登录,再进行评论。

采纳的回答

Kaustubha Govind
Kaustubha Govind 2012-3-6
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 个评论
Richard Crozier
Richard Crozier 2012-3-7
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by