Hi... I converted a basic adding program from matlab .m function file into .c & .h file. When i try to include the header file function "add" into my main function in dev-c++ 4.9.9.2 --- [Linker error] undefined reference to `add' is the error message on compilation.
Here is my code
Main function code:

#include "add.h"
#include <conio.h>
main()
{
   real_T x,y,z;
    printf("Enter two numbers to add\n");
    scanf("%lf%lf",&x,&z);
    y =  add(x,z);
    printf("Sum of entered numbers = %lf\n",y);
    getch();
    //return 0;
    }

add.h Header file code

#ifndef __ADD_H__
#define __ADD_H__
/* Include files */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "rtwtypes.h"
#include "add_types.h"

/* Type Definitions */

/* Named Constants */

/* Variable Declarations */

/* Variable Definitions */

/* Function Declarations */
//extern real_T add(real_T x, real_T z);
real_T add(real_T x, real_T z);
#endif
/* End of code generation (add.h) */

FYI : rtwtypes.h has got all the data type defined using typedef....! for double its **real_T **....!

Thanks for your time and valuable suggestions!

Recommended Answers

All 2 Replies

real_T add(real_T x, real_T z);

And where is add() defined? The error is saying that you have a declaration for the function, but no definition exists and therefore it cannot be called.

As i mentioned earlier -- i tried n used matlab to generate these c files ...! I have got add.c (source) / add.h , rtwtypes.h (all datat types are typedefined), add_types.h , add_intialize.h , add_terminate.h ...!
I think including add.h will be more enough to call the add() function...!
anyway here is the source add.c which i got frm matlab....

add.c

#include "rt_nonfinite.h"
#include "add.h"


real_T add(real_T x, real_T z)
{
  return x + z;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.