How to compile a library for linking with a mexfunction?

4 ビュー (過去 30 日間)
Richard Crozier
Richard Crozier 2012 年 3 月 1 日
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 Center および File ExchangeWrite 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