Compiling with Mex - Can I reference Environment Variables?

I'm attempting to compile a Simulink S-Function with Mex, which #includes some header files that are from another directory on our system. This directory is defined in an environment variable.
Is it possible to reference the environment variable in the mex -I or -L options?
e.g. mex -I$(REF_DIR)/include my_sfunction.c
On Windows, this is tricky as you refer to environment variables using %REF_DIR%, which won't work in Matlab, as % signifies a comment
Do I just have to bite the bullet and put in the full path? Or edit mexopts.bat?

Answers (2)

Try:
mex -v COMPFLAGS="$COMPFLAGS -I%REF_DIR%/include" my_sfunction.c
I think this command, or a minor variation on it should work.

1 Comment

Sorry, didn't work - the % sign, even inside double quotes is being interpreted as the start of a comment, and the rest of the line is ignored:
>> mex -v COMPFLAGS="$COMPFLAGS -I%REF_DIR%/include" my_sfunction.c
Unmatched double quote: "$COMPFLAGS -I
??? Error using ==> mex
Unable to complete successfully
Thanks for the suggestion, though.

Sign in to comment.

Have you tried the function version of the mex command? E.g.,
mex('-v','COMPFLAGS="$COMPFLAGS -I%REF_DIR%/include"','my_sfunction.c')
This will force the arguments with embedded spaces to be interpreted by the mex command as single arguments instead of multiple arguments. You might need to make the 'COMPFLAGS=' a separate argument from the "etc" part ... not sure about this.

3 Comments

Thanks for the suggestion, but again no dice. I suspect the problem for me now is that %REF_DIR% contains a space (it's C:\Program Files\Ref) so I realistically need to enclose it in double quotes on the cl command line, e.g.
mex('-v', 'COMPFLAGS="$COMPFLAGS -I"%REF_DIR%"/include"', 'my_sfunction.c')
but for the life of me, I can't figure out how to escape the double quotes within the double quotes. Backslash isn't going to work as it's interpreted as a Windows directory separator.
I even tried the following:
mex('-v', '-I"%REF_DIR%"/include', 'my_sfunction.c')
but mex/Matlab removes the double quotes in this case, and although it does substitute the environment variable, the cl line fails due the space in the filename path.
Can you double up the double quotes inside the double quotes? e.g.,
"$COMPFLAGS -I""%REF_DIR%""/include"
use simple quote instead of double quote: mex -v COMPFLAGS='$COMPFLAGS -I%REF_DIR%/include' my_sfunction.c

Sign in to comment.

Categories

Find more on Simulink Coder in Help Center and File Exchange

Asked:

on 7 Sep 2011

Community Treasure Hunt

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

Start Hunting!