How do I link against external library files when compiling my MATLAB file with the MATLAB Compiler?

19 views (last 30 days)
I have a an MATLAB file that calls an external C/C++ function using the #%external pragma. I want to include additional external header files header1.h and header2.h located at C:\myproj\include\ and and link against library files lib1.lib and lib2.lib located at C:\myproj\lib\.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Jan 2010
In order to link against external LIB-files, you will need to modify your compiler options file, which is created upon running MBUILD -SETUP. Perform the following steps, depending on which platform you are compiling on.
The options file can be found in the directory returned by the command PREFDIR and is called COMPOPTS.BAT on Windows and mbuildopts.sh on UNIX/Mac.
1a) On Windows, to add a new include directory, add it to the end of the INCLUDE flag, for example:
INCLUDE=%MSVCDir%\INCLUDE;%MSVCDir%\MFC\INCLUDE;%MSVCDir%\ATL\INCLUDE;%INCLUDE%;C:\myproj\include;
To add a new LIB-file, first add the directory containing the libraries to the LIB flag, for example:
set LIB=%MSVCDir%\LIB;%MSVCDir%\MFC\LIB;%LIB%
Then add the name of the library to the LINKFLAGS file:
set LINKFLAGS=kernel32.lib user32.lib gdi32.lib advapi32.lib oleaut32.lib ole32.lib /LIBPATH:"%LIBLOC%" /nologo
set LINKFLAGS=%LINKFLAGS% mclmcrrt.lib lib1.lib lib2.lib
1b) On UNIX/Mac systems, to add a new include directory, modify the CFLAGS for C applications or CXXFLAGS for C++ applications:
CFLAGS="$MFLAGS -ansi -D_GNU_SOURCE -pthread -fexceptions -I/usr/myinclude"
To add the library, modify the CLIBS flag (or CXXLIBS flag). To link libMyLib.lib, you would add
CLIBS="$RPATH $MLIBS -lm -lstdc++ -lmyLib"
Lastly add the directory containing the library to the LDFLAGS flag:
LDFLAGS='-pthread -L/mydir/mydirwithlibs'
2) Once you have made changes to your options file based on the platform you are on, save the changes to a filename of your choosing and compile your code specifying the new options file that you have created:
mcc -mv mymain.m myCfile.c -f <full_path_to_new_options_file>
  1 Comment
Kaustubha Govind
Kaustubha Govind on 18 Mar 2014
Edited: MathWorks Support Team on 26 Sep 2018
Sumanth: Please see Custom Building MEX-Files for information on compiling MEX-files. This solution is specific to the use of MCC, which is part of the MATLAB Compiler toolbox.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!