MATLAB: Correct syntax for building MEX files in 2014a using trailing backslash \ in file path names

MATLAB

I mex my C file as follows:
>>  mex -output test -DBASE_PATH_NAME="T:\\directory\\" test.c
The file is mexed successfully in MATLAB R2013b. However, it fails in MATLAB R2014a with the following error:
Error using mex
test.c
T:\directory\test.c(9) : error C2001: newline in constant
T:\directory\test.c(11) : error C2143: syntax error : missing ';' before 'type'

Best Answer

As of R2014a, MATLAB supports single quotes (') to override a default compiler switch. For example,
 
>> mex -output test -DBASE_PATH_NAME='c:\\foo\\' test.c
 
If you use double quotes (") instead of single quotes, the compiler interprets \" as an escape sequence. If you want to add a trailing backslash character to a path name, you must add the backslash escape character:
 
For example, replace:
>> mex -output test -DBASE_PATH_NAME="c:\\foo\\" test.c
 
with:
>> mex -output test -DBASE_PATH_NAME="c:\\foo\\\\" test.c
As this adds unnecessary characters it is best practice to adopt the new format of using single quotes in specifying arguments passed to the compiler.
Related Question