Using MEX compiler options "-Dname=value"
조회 수: 11 (최근 30일)
이전 댓글 표시
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
채택된 답변
Pierre
2011년 8월 5일
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
추가 답변 (2개)
Kaustubha Govind
2011년 8월 5일
See Override Option Details in the documentation for the MEX command. This is how you should specify custom compiler/linker flags.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 MATLAB Compiler에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!