Using MEX compiler options "-Dname=value"
11 views (last 30 days)
Show older comments
Hi,
I am trying to compile a mex cpp function which I need to define a preprocessor symbol (i.e. mex -DXX="I AM A STRING" xxx.cpp), but I cannot figure out how to get the value to be sent to the actual compiler as a string. I am running Matlab R2009b on Solaris. I am using (all of which do not work):
mex('-f',mymexoptsfile,sprintf('-DXX=%s',valueName));
mex('-f',mymexoptsfile,sprintf('-DXX="%s"',valueName));
mex('-f',mymexoptsfile,sprintf('-D''XX="%s"''',valueName));
Thank you. Jordan
0 Comments
Accepted Answer
Pierre
on 5 Aug 2011
I just got some limited example running:
#include <mex.h>
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define THESTRING TOSTRING(MYSTRING)
void mexFunction(int nout, mxArray *out[], int nin, const mxArray *in[])
{
mexPrintf(THESTRING);
}
If you compile this with the option -DMYSTRING=Hello, the mex will print "Hello" to the console. A hard limitation though is that I couldn't figure out how to include whitespaces in the string.
void mexFunction(int nout, mxArray *out[], int nin, const mxArray *in[])
{
mexPrintf(MYSTRING);
}
The behaviour of the error messages I get while struggling to pass entire strings including their quotes, is that, in fact, the quotes "don't reach" to the compiler because they are stripped off through several layers of interpretation, notably the (at least on Linux) bash script which gets rid of some doublequotes. Perhaps this might even happen multiple times, such that, you would have to cascade-escape the doublequotes by escaping some scape-characters too. But I couldn't figure out the right sequence yet, but it should be something like -DMYSTRING=\\\\"\\\"Hello World\\\"\\\\"... quite tricky! :S
0 Comments
More Answers (2)
Kaustubha Govind
on 5 Aug 2011
See Override Option Details in the documentation for the MEX command. This is how you should specify custom compiler/linker flags.
0 Comments
See Also
Categories
Find more on MATLAB Compiler in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!