Undefined reference to log10?

2k Views Asked by At

I have the following two functions and when compiling, I am face with the error in the title.

double getIdf(FileList fl, int D){
  double fileCount = countFiles(fl); //todo countfiles
  double temp = fileCount/D;
  double idf = log10(temp);
  return (fabs(idf));
}

double countFiles(FileList fl){
    if (fl == NULL){
        printf("countFiles FL does not exist\n");
        return 0;
    }
    double count = 0;
    FileList curr = fl;
    while (curr != NULL) {
        count++;
        curr = curr->next;
    }
    return count;
}

However, if I change the log10 function to log10(5.5) it would work.

double getIdf(FileList fl, int D){
  double fileCount = countFiles(fl); //todo countfiles
  double temp = fileCount/D;
  double idf = log10(5.5);
  return (fabs(idf));
}

I am compiling with -lm.

Whats wrong here?

1

There are 1 best solutions below

2
Joshua On

You typed

gcc -lm program.c

You want

gcc program.c -lm

Because libraries only pull in symbols that are referenced by the code already being linked in, and you tried to link in the library before adding any code.

I know, it's terrible. It would be nicer if it could index the libraries and use them to reference symbols needed only after the library loads. I've had to reference libraries twice.