Effacer les filtres
Effacer les filtres

Using MEX compiler options "-Dname=value"

11 vues (au cours des 30 derniers jours)
Jordan
Jordan le 5 Août 2011
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

Réponse acceptée

Pierre
Pierre le 5 Août 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.
( Source )
ADDENDUM: OK, now I got your question, it's more like: #include mex.h
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

Plus de réponses (2)

Kaustubha Govind
Kaustubha Govind le 5 Août 2011
See Override Option Details in the documentation for the MEX command. This is how you should specify custom compiler/linker flags.

Jordan
Jordan le 5 Août 2011
Pierre, your addendum assumption was more correct. I needed to determine the right escape sequence. The following works, but couldn't tell you why:
mex('-f',mymexoptsfile,sprintf('-DXX=\"\\\\\\\"%s\\\\\\\"\"',valueName), 'mycpp.cpp');
I think the outside \" are needed for the first string, the remaining are needed to get \" inside the compiler line:
.../g++ -c -DXX=\"STRING\" mycpp.cpp

Catégories

En savoir plus sur MATLAB Compiler dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by