필터 지우기
필터 지우기

How to compile a library for linking with a mexfunction?

조회 수: 2 (최근 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 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