2

This is function pointer program: demo.c

#include <stdio.h>
#include <math.h>

void tabulate(double (*f)(double), double first, double last, double incr);

int main(void) {

    double final, increment, initial;

    printf("Enter initial value: ");
    scanf("%lf", &initial);

    printf("Enter final value: ");
    scanf("%lf", &final);

    printf("Enter increment: ");
    scanf("%lf", &increment);

    printf("\n      x       cos(x)"
           "\n   -------    -------\n");
    tabulate(cos, initial, final, increment);
    return 0;
}

void tabulate(double (*f)(double), double first, double last, double incr) {
    double x;
    int i, num_intervals;

    num_intervals = ceil((last - first)/incr);
    for(i=0; i<=num_intervals; i++) {
        x = first + i * incr;
        printf("%10.5f %10.5f\n", x, (*f)(x));
    }
}

When I try to run this code, got this message:

/tmp/ccQ4IZeD.o: In function `main':
demo.c:(.text+0x92): undefined reference to `cos'
/tmp/ccQ4IZeD.o: In function `tabulate':
demo.c:(.text+0xe3): undefined reference to `ceil'
collect2: error: ld returned 1 exit status

What does it means, because cos and ceil is <math.h> file function. Is this is because of OS (ubuntu 13.10 32-bits), because this program is work on else system.

If it yes, then what solution is to run on my system.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Rohit
  • 83
  • 1
  • 2
  • 8

3 Answers3

9

You have to use -lm at link time.

jmlemetayer
  • 4,774
  • 1
  • 32
  • 46
4

Due to math library are not integrated in the standard gcc library because of integration issues with the kernel. you have to use -lm while compiling the code

1

This is because your math library functions are not linked with the program. To link it compile with -lm flag.

haccks
  • 104,019
  • 25
  • 176
  • 264