本文介绍了如何打印像__LINE__ C- preprocessor变量与mexErrMsgTxt()在Matlab中MEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关调试Matlab的MEX,它可以是一个相当麻烦,这将是很好有更好的断言能力。继this关于MEX-断言的问题,这是可以定义preprocessor万客隆,抛出一个错误Matlab和打印的字符串(可代替大部分 mxAssert ,不幸崩溃Matlab2011b)。

For debugging Matlab-MEX, which can be quite a hassle, it would be nice to have better assertion capabilities. Following this question about mex-assertions, it is possible to define a preprocessor makro, that throws an error to Matlab and prints a string (can mostly replace mxAssert, which unfortunately crashes Matlab2011b).

#define myassert( isOK,astr )      ( (isOK) ? (void)0 : (void) mexErrMsgTxt(astr) ) 

这将是好得多要打印的文件,行号和调用函数,从那里下面的例子中断言 myassert(A = B,A不是B)是上调! This回答到初始状态的问题,他们是preprocessor变量:

It would be much nicer to print the file, line number and caller function, from where following example assertion myassert(A=B,"A not B") is raised! This answer to the initial question states that they are the preprocessor variables:

__LINE__,__PRETTY_FUNCTION__, __FILE__

我们如何能够打印这些preprocessor变量与 mexErrMsgTxt

How can we print these preprocessor variables with mexErrMsgTxt?

问题是, mexErrMsgTxt()需要一个char *参数,而不是多个输入例如像的printf(为const char *格式。 ..)

The problem is, that mexErrMsgTxt() takes a char* argument and not multiple inputs like for example printf(const char *format, ...).

我的想法只能到此为止的时刻:

My thinking goes only so far at the moment:


  1. 这是不可能建立一个功能,因为preprocessor变量将从功能有值(如行号)。

  2. 这是不可能为我编写创建工作多行preprocessor万客隆一个字符从通过字符串而aStr ,并把它传递给 mexErrMsgTxt()。 <一href=\"http://stackoverflow.com/questions/1204202/is-it-possible-to-print-a-$p$pprocessor-variable-in-c\">Maybe一个解决方案是沿着这些线路。

  3. A 混合的使用char创建preprocessor万客隆并把它传递给 mexErrMsgTxt()函数的解决方案不一样的感觉良好的编码习惯。

  1. It's not possible to build a function, because the preprocessor variables will have the values (e.g. line number) from the function.
  2. It was not possible for me to write a working multiline preprocessor makro that creates a char from the passed string astr and passes it to mexErrMsgTxt(). Maybe a solution is along these lines.
  3. A hybrid solution with a char creating preprocessor makro and a function that passes it to mexErrMsgTxt() doesn't feel like good coding practice.

这将是非常好的,使指定的错误字符串可选。

It would be really nice to make the specified error string optional.

推荐答案

串联preprocessor令牌的工作,只要你只使用 __ FILE __ __ __ LINE 和一个字符串文字作为消息。然后,你可以写的东西像

Concatenating preprocessor tokens works as long as you only use __FILE__, __LINE__ and a string literal as message. Then you can write something like

#define STRINGIZE_I(x) #x
#define STRINGIZE(x) STRINGIZE_I(x)

#define myassert(isOK, astr) ( (isOK) ? (void)0 : (void) mexErrMsgTxt(__FILE__ ":" STRINGIZE(__LINE__) ": " astr) )

不幸的是, __ preTTY_FUNCTION __ 不是即使对那些谁支持它的编译器字符串文字。如果你想使用它(或更少固定的错误信息),你就必须动态地组装线,这意味着沿着

Unfortunately, __PRETTY_FUNCTION__ is not a string literal even for those compilers who support it. If you want to use it (or less fixed error messages), you'll have to assemble the string dynamically, which means something along the lines of

#define myassert(isOK, astr)                                            \
  do {                                                                  \
    if(!(isOk)) {                                                       \
      std::ostringstream fmt;                                           \
      fmt << "In " << __PRETTY_FUNCTION__ << ", "                       \
          << __FILE__ << ":" << __LINE__ << ": " << (astr);             \
      (void) mexErrMsgTxt(fmt.str().c_str());                           \
    }                                                                   \
  } while(false)

有关C,做的snprintf 相同。 (或 asprintf ,它避免了与固定的缓冲区长度长的错误消息的问题,它是关于一样便携 __ preTTY_FUNCTION __ )。无论哪种方式,大致是

For C, do the same with snprintf. (Or asprintf. It avoids problems with fixed buffer lengths and long error messages, and it is about as portable as __PRETTY_FUNCTION__). Either way, roughly like

#define myassert(isOK, astr)                                            \
  do {                                                                  \
    if(!(isOk)) {                                                       \
      char buf[ENOUGH_SPACE];                                           \
      snprintf(buf, ENOUGH_SPACE, "In %s, %s:%d: %s",                   \
               __PRETTY_FUNCTION__, __FILE__, __LINE__, (astr));        \
      buf[ENOUGH_SPACE - 1] = '\0';                                     \
      (void) mexErrMsgTxt(buf);                                         \
    }                                                                   \
  } while(0)

...其中 ENOUGH_SPACE 将不得不进行适当定义(在的snprintf 情况)。

...where ENOUGH_SPACE would have to be defined appropriately (in the snprintf case).

这篇关于如何打印像__LINE__ C- preprocessor变量与mexErrMsgTxt()在Matlab中MEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 03:59