1

I get this error and I'm not sure how to fix it. This is a project for information retrieval where i am trying to calculate tf-idf using this type (1+log(freq(t,n)))*log(N/k). freq(t,n) is the frequency of a word, t in file n and N is the number of total files, k number of files that contain the word t.

 Undefined                       first referenced
 symbol                             in file
log                                 /var/tmp//ccx8E8Y1.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

here is my fuction where I get the error (i have #include <math.h> in the start):

void makeTF_IDF(char** words,double** weight,char** str){
    int i,j,n,f;
    char nameout[1024],line[1024];
    double tf,idf[1443],t;
    FILE *fin;
    for(i=0;i<1443;i++){
        n=0;
        for(j=0;j<26;j++){
            strcpy(nameout,strtok(str[j],"."));
            strcat(nameout,"out.txt");
            fin=fopen(nameout,"r");
            while(1){
                if(fgets(line,1024,fin)==NULL) break;
                if(strstr(line,words[i])!=NULL){
                    n++;
                    break;
                }
            }
            fclose(fin);
        }
        t=26/n;
        idf[i]=log(t);
    }
    for(i=0;i<1443;i++){
        for(j=0;j<26;j++){
            f=0;
            strcpy(nameout,strtok(str[j],"."));
            strcat(nameout,"out.txt");
            fin=fopen(nameout,"r");
            while(1){
                if(fgets(line,1024,fin)==NULL) break;
                if(strstr(line,words[i])!=NULL) f++;
            }
            weight[j][i]=(log(1+f))*idf[i];
            fclose(fin);
        }
    }
}
  • Thank you so much, i didn't know that i had to compile it this way. I am an IT student and I haven't worked that much in C and specially compiling-running executives using terminal., i mostly use various IDE. – marinos theo Nov 26 '15 at 12:55
  • the question says: calculate tf-idf using this type (1+log(freq(t,n)))*log(N/k). freq(t,n) " but the variable `tf` is not used in the posted code – user3629249 Nov 29 '15 at 03:47
  • for readability and understandability of the code by us humans, please follow the following guidelines: only one statement per line. at max only one variable declaration per statement. separate code blocks (for, if, else, do...while, whlie, switch, case, default) by a blank line – user3629249 Nov 29 '15 at 03:49
  • the posted code contains several 'magic' numbers. 'magic' numbers make the code much harder to read/understand/debug/maintain. So examples from the posted code: 1024, 1443, 26, 8. Suggest using #define's or an enum to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code. Note: in the calls to `fgets()` the second parameter should be `sizeof(line)` so the size of the variable `line[]` can be changed without introducing unnecessary bugs into the program – user3629249 Nov 29 '15 at 03:56
  • this kind of line: `if(strstr(line,words[i])!=NULL) f++;` does not search the `**words` array of pointers to character arrays for any match for each `word[]` and since each pass through the surrounding while(1) loop is looking at a new line. – user3629249 Nov 29 '15 at 04:02
  • the returned value from this line: `fin=fopen(nameout,"r");` needs to be checked (!=NULL) to assure the operation was successful before trying to read from the associated file. – user3629249 Nov 29 '15 at 04:03

1 Answers1

1

I suppose you are working on a unix environment (and if you are trying to make an executable out of this file only, you do have a main function).

You should compile with a command like this in order to search for the math library when linking:

gcc <your_filename.c> -lm

You should have an executable named a.out in your current working directory after this command

sestus
  • 1,875
  • 2
  • 16
  • 16