0

I wrote this function

int *constructST(int arr[], int n)
{
    // Allocate memory for segment tree
    int x = (int)(ceil(log2(n))); //Height of segment tree
    int max_size = 2*(int)pow(2, x) - 1; //Maximum size of segment tree
    int *st = malloc(max_size*sizeof(int));

    // Fill the allocated memory st
    constructSTUtil(arr, 0, n-1, st, 0);

    // Return the constructed segment tree
    return st;

}

and I have included the following libraries math.h ,stdlib.h, stdio.h but I get the following error

/tmp/ccg4X72c.o: In function `constructST':
tree.c:(.text+0x3f4): undefined reference to `log2'
tree.c:(.text+0x40b): undefined reference to `ceil'
tree.c:(.text+0x433): undefined reference to `pow'
collect2: error: ld returned 1 exit status

Any help why I am getting this error though I have included the math.h .

Anoop Kanyan
  • 618
  • 7
  • 19
  • "Any help why I am getting this error though I have included the math.h" - it seems that you don't know what the difference between compilation and linkage is. Know it. (I. e. google them.) –  Dec 11 '13 at 16:09

1 Answers1

3

Including <math.h> ensures that your program knows the function prototypes for these functions, so it can tell that you're calling them correctly, but it doesn't actually link the library code in to them. For that you will have to add the right linker flag when you build it, usually -lm:

gcc -o myprog myprog.c -lm
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Tim Pierce
  • 5,514
  • 1
  • 15
  • 31