undefined reference to `rtIsNaN'

2019-08-10 05:33发布

I am trying my hands with Matlab coder. I wrote a simple matlab script and generated the C-code (along with the necessary headers) for the same. The matlab script looks like this :

function amin_idx=findminidx(a)%#codegen
amin_idx=find(a==min(a),1,'first');

In the Matlab coder, I start a new project, pre-condition a as a 10x1 vector and build the project to generate the .c and .h files. The I write a new .c file containing the main() which is as follows :

#include<stdio.h>
#include "rt_nonfinite.h"/*from generated code*/
#include "findminidx.h"/*from generated code*/

void main(void){
  double a_data[3]={6,5,4};
  int a_size[2]={3,1};
  double amin_idx_data[1]={-1};
  int amin_idx_size[2]={1,1};
  findminidx(a_data,a_size,amin_idx_data,amin_idx_size);/*from generated code*/
  printf("\namin_idx = %f\n",amin_idx_data[0]);
}

The .c file (generated) containing the findminidx() is given below :

/*
 * File: findminidx.c
 *
 * MATLAB Coder version            : 2.6
 * C/C++ source code generated on  : 30-Aug-2014 18:04:42
 */

/* Include files */
#include "rt_nonfinite.h"
#include "findminidx.h"

/* Function Definitions */

/*
 * Arguments    : const double a_data[]
 *                const int a_size[2]
 *                double amin_idx_data[]
 *                int amin_idx_size[2]
 * Return Type  : void
 */
void findminidx(const double a_data[], const int a_size[2], double
                amin_idx_data[], int amin_idx_size[2])
{
  int ixstart;
  double mtmp;
  int ix;
  boolean_T exitg2;
  boolean_T x_data[10];
  int k;
  int ii_data[1];
  boolean_T exitg1;
  int b_ii_data[1];
  ixstart = 1;
  mtmp = a_data[0];
  if (a_size[1] > 1) {
    if (rtIsNaN(a_data[0])) {
      ix = 2;
      exitg2 = false;
      while ((!exitg2) && (ix <= a_size[1])) {
        ixstart = ix;
        if (!rtIsNaN(a_data[ix - 1])) {
          mtmp = a_data[ix - 1];
          exitg2 = true;
        } else {
          ix++;
        }
      }
    }

    if (ixstart < a_size[1]) {
      while (ixstart + 1 <= a_size[1]) {
        if (a_data[ixstart] < mtmp) {
          mtmp = a_data[ixstart];
        }

        ixstart++;
      }
    }
  }

  ixstart = a_size[0] * a_size[1];
  for (ix = 0; ix < ixstart; ix++) {
    x_data[ix] = (a_data[ix] == mtmp);
  }

  if (1 <= a_size[1]) {
    k = 1;
  } else {
    k = 0;
  }

  ixstart = 0;
  ix = 1;
  exitg1 = false;
  while ((!exitg1) && (ix <= a_size[1])) {
    if (x_data[ix - 1]) {
      ixstart = 1;
      ii_data[0] = ix;
      exitg1 = true;
    } else {
      ix++;
    }
  }

  if (k == 1) {
    if (ixstart == 0) {
      k = 0;
    }
  } else {
    if (1 > ixstart) {
      ixstart = -1;
    } else {
      ixstart = 0;
    }

    ix = 0;
    while (ix <= ixstart) {
      b_ii_data[0] = ii_data[0];
      ix = 1;
    }

    k = ixstart + 1;
    ixstart++;
    ix = 0;
    while (ix <= ixstart - 1) {
      ii_data[0] = b_ii_data[0];
      ix = 1;
    }
  }

  amin_idx_size[0] = 1;
  amin_idx_size[1] = k;
  ixstart = k;
  ix = 0;
  while (ix <= ixstart - 1) {
    amin_idx_data[0] = ii_data[0];
    ix = 1;
  }
}

/*
 * File trailer for findminidx.c
 *
 * [EOF]
 */

The intention is, calling the .c routine generated from matlab, passing it a small array and printing the index of minimum value of the passed array. I put the c-file containg main() in the same directory as the one containing all matlab generated .c and .h files. Also, I included the respective headers in the main() c file as can be seen above. When I try to compile using gcc file1.c findminidx.c, it yields the following error.

undefined reference to `rtIsNaN'

I can not understand why is this? My system details are as follows :

  1. Ubuntu 14.04, 64 bit
  2. Matlab R2014a, 64 bit
  3. gcc version 4.8.2

2条回答
Anthone
2楼-- · 2019-08-10 06:02

If you are going to use the generated code on the same machine on which you are running MATLAB, you can simply let Coder compile all of the generated code for you by having it generate a static library (.a file). Choose "static library" on the output type dropdown in the Coder project. Coder will generate the C code and compile all of it into a static library, findminidx.a, in the directory where all of the code is generated.

Then you can just link against that library rather than needing to compile all of the generated code manually:

gcc main.c fminidx.a -o fminidx

Alternatively, you can choose "executable" on the build tab of the project and specify your main.c in the Coder project. After code is generated, a standalone executable will be compiled using your provided main function.

查看更多
孤傲高冷的网名
3楼-- · 2019-08-10 06:16

You've #included rt_nonfinite.h (as required), but you are not telling the compiler to use rt_nonfinite.c in your compile line.

查看更多
登录 后发表回答