How to use a variable in the mex compiling code sentence?

53 visualizaciones (últimos 30 días)
Laura
Laura el 12 de En. de 2026 a las 19:00
Editada: Laura el 12 de En. de 2026 a las 22:29
I am currently using the following mex compiling code sentence (successfully):
mex GCC='/usr/bin/gcc-11' -I/usr/local/include -L/usr/local/lib -lsundials_cvode -lsundials_nvecserial -lm MyFile_cvode.c
I would like to update my code to specify the file MyFile_cvode.c using a variable. i.e. something like
FileChoice=MyFile_cvode.c;
mex GCC='/usr/bin/gcc-11' -I/usr/local/include -L/usr/local/lib -lsundials_cvode -lsundials_nvecserial -lm FileChoice
I've read that in order to accomplish this, I need to switch from using sentence syntax to function syntax. However, I'm very new to mex and I don't understand the syntax well enough to understand how to make the conversion correctly.
I've tried:
mex('GCC="/usr/bin/gcc-11"','-I/usr/local/include','-L/usr/local/lib','-lsundials_cvode', '-lsundials_nvecserial', '-lm', 'MyFile_cvode.c')
but this produced errors.

Respuesta aceptada

Laura
Laura el 12 de En. de 2026 a las 20:30
Editada: Laura el 12 de En. de 2026 a las 22:29
Answered my own question!
Syntax should have been
mex( 'MyFile_cvode.c','GCC="/usr/bin/gcc-11"','-I/usr/local/include','-L/usr/local/lib','-lsundials_cvode', '-lsundials_nvecserial', '-lm')
or
FileChoice='MyFile_cvode.c';
mex(FileChoice,'GCC="/usr/bin/gcc-11"','-I/usr/local/include','-L/usr/local/lib','-lsundials_cvode', '-lsundials_nvecserial', '-lm')
i.e. file to be compiled listed first.

Más respuestas (1)

dpb
dpb el 12 de En. de 2026 a las 20:31
Editada: dpb el 12 de En. de 2026 a las 20:32
function mymex(file)
% build and execute mex for given input file
cmd="GCC='/usr/bin/gcc-11' -I/usr/local/include -L/usr/local/lib -lsundials_cvode -lsundials_nvecserial -lm ";
cmd=cmd + file; % append the input file to compile
mex(cmd) % dispatch to mex
end
CAUTION: AIR CODE! Don't have mex compiler installed at the moment so can't do a hard test.
The problem in your attempt is mex command is one long string, not a set of independent input parameters so separating the arguments with commas isn't correct synatax. Then, you enclosed your filename in quotes so it became a fixed constant, not substituting in the variable name.
The above takes all the command line fixed arguments and then adds the input file name as one long string; then calls mex as a function with that long commnd line. I did test that mex() will accept the string variable by running
cmd="-help";
mex(cmd)
MEX Compile MEX-function Usage: mex [options ...] file [files ...] Description: MEX compiles and links source files into a shared library called a MEX-file, executable from within MATLAB. It also builds executable files for standalone MATLAB engine and MAT-file applications. Command Line Options Available on All Platforms: -c Compile only. Creates an object file but not a MEX-file. -client engine Build standalone MATLAB engine or MAT-file application. -compatibleArrayDims Build a MEX-file using the MATLAB Version 7.2 array-handling API, which limits arrays to 2^31-1 elements. -D<name> Define a symbol name to the C preprocessor. Equivalent to a "#define <name>" directive in the source. Do not add a space after this switch. -D<name>=<value> Define a symbol name and value to the C preprocessor. Equivalent to a "#define <name> <value>" directive in the source. Do not add a space after this switch. -f <optionsfile> For advanced users. Specify location and name of the MEX configuration file to use. Overrides MEX's default compiler selection mechanism. -g Adds symbolic information and disables optimizing built object code. Use for debugging. -h[elp] Display this message. -I<pathname> Add <pathname> to the list of directories to search for #include files. Do not add a space after this switch. -l<name> Link with object library. On Windows, <name> expands to "<name>.lib" or "lib<name>.lib". On Linux, to "lib<name>.so". On Mac, to "lib<name>.dylib". Do not add a space after this switch. -L<folder> Add <folder> to the list of folders to search for libraries specified with the -l option. Do not add a space after this switch. -largeArrayDims Build a MEX-file using the MATLAB large-array-handling API. This API can handle arrays with more than 2^31-1 elements when compiled on 64-bit platforms. -R2017b Build a MEX-file using the MATLAB large-array-handling, and graphics-objects APIs. This option is available on 64-bit platforms. The API can handle arrays with more than 2^31-1 elements. -R2017b is the default option. -R2018a Build a MEX-file using the MATLAB large-array-handling, graphics-objects, and interleaved-complex APIs. This option is available on 64-bit platforms. The API can handle arrays with more than 2^31-1 elements, and supports type-safe and interleaved complex data access. -n No execute mode. Display commands that MEX would otherwise have executed, but do not actually execute any of them. -O Optimizes the object code. Use this option to compile with optimization. Optimization is enabled by default. Specify this option with the capital letter O. -outdir <dirname> Place all output files in folder <dirname>. -output <resultname> Create MEX-file named <resultname>. The appropriate MEX-file extension is automatically appended. Overrides MEX's default MEX-file naming mechanism. -setup <lang> Change the default compiler to build <lang> language MEX-files. When this option is specified, no other command line input is accepted. -silent Suppress informational messages. The mex function still reports errors and warnings, even when you specify -silent. -U<name> Remove any initial definition of the C preprocessor symbol <name>. (Inverse of the -D option.) Do not add a space after this switch. -v Verbose mode. Display the values for important internal variables after all command line arguments are considered. Displays each compile step and final link step fully evaluated. <name>=<value> Override default setting for variable <name>. This option is processed after all command line arguments are considered. Command Line Options Available Only on Windows Platforms: @<rspfile> Include contents of the text file <rspfile> as command line arguments to MEX. For more information, see http://www.mathworks.com/help/matlab/ref/mex.html
which shows mex was called with the passed argument string.
Use as
mymex('MyDesiredFile.c')

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by