MATLAB: Do I get a compilation error when I compile the Real-Time Workshop 6.6 (R2007a) generated code containing ‘_isnan’ with the hand-written code

_isnancisnanmicrosoftmsmsvcrt_rtisnansimulink codervcvisual

When I then try to link my hand-written code with Real-Time Workshop generated code that contains 'rt_nonfinite.c' , I get the following error at compile time:
./fadec_autocode/fadec_src/lib/libfadec.a(rt_nonfinite.o): In function
`rtIsNaN':
/sim/sandbox/AE1107/C/src/fadec_autocode/fadec_src/rt_nonfinite.c:170:
undefined reference to `_isnan'
./fadec_autocode/fadec_src/lib/libfadec.a(rt_nonfinite.o): In function
`rtIsNaNF':
/sim/sandbox/AE1107/C/src/fadec_autocode/fadec_src/rt_nonfinite.c:180:
undefined reference to `_isnan'
Previous uses of real-time workshop on a different version of the same simulink model
yielded different results for the rtIsNaN and rtIsNaNF functions.
Previously, the real-time workshop output rtIsNan and rtIsNaNF as shown
below and did not actually use _isnan:
/* Function: rtIsNaN ==================================================
* Abstract:
*Test if value is not a number
*/
boolean_T rtIsNaN(real_T value)
{
return((value!=value) ? 1U : 0U);
}
As opposed to what I'm getting now that is causing the issue:
/* Function: rtIsNaN ==================================================
* Abstract:
*Test if value is not a number
*/
boolean_T rtIsNaN(real_T value)
{
return _isnan(value)? TRUE:FALSE;
}
Why is the implementation different now? What do I do to resolve this situation?

Best Answer

The different implementation of 'rtIsNan' function is due to using Microsoft Visual C++ compiler during code generation. If Microsoft Visual Compiler is used for code generation, then RTW generated code contains reference to _isnan() function which is shipped with the compiler.
This can be verified in the MSDN documentation page below:
<http://msdn.microsoft.com/en-us/library/aa298428%28VS.60%29.aspx>
For example, following is the generated code with Real-Time Workshop R2007a, R2007b, R2008a and R2008b, when Microsoft Visual C++ 2005 was NOT selected as the compiler:
boolean_T rtIsNaN(real_T value)
{
return((value!=value) ? 1U : 0U);
}
Following is the generated code in R2007a,R2007b and R2008a, when Microsoft Visual C++ 2005 was the selected compiler:
boolean_T rtIsNaN(real_T value)
{
/* For MSVC 6.0, must use the compiler specific comparison function */
return _isnan(value)? 1U:0U;
}
As you can observe above, when MS Visual Compiler is used, there is a reference to _isnan. Therefore when you package this into an archive for compilation using a different compiler, this would result in an error.
However in R2008b, even with MS VC as the selected compiler, the code generated contains appropriate pre-processor directives to handle this situation. Following is the code generated in R2008b when MS VC is selected as compiler::
boolean_T rtIsNaN(real_T value)
{
#if defined(_MSC_VER) && (_MSC_VER <= 1200)
/* For MSVC 6.0, use a compiler specific comparison function */
return _isnan(value)? 1U:0U;
#else
return((value!=value) ? 1U : 0U);
#endif
}
_MSC_VER is MS VC specific directive explained in the link below:
<http://msdn.microsoft.com/en-us/library/b0084kay.aspx>
To summarize, there are two workarounds to this issue:
1. Execute "mex -setup" in your MATLAB and change the current compiler to a non-Microsoft compiler.
2. Please generate code using R2008b or later versions.