Thread: Linker error???

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Linker error???


    i am building a program that finds the longest path to calculate the solution...

    this is the code:





    #include <stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #define _GNU_SOURCE

    int startIndex = 0;

    int countOneBranchLength(int** mtrx, int rowcount, int curRow){
    int res = 1;
    int i = startIndex;
    while (i < rowcount) {
    if (mtrx[i][1] == mtrx[curRow][2]){
    int nodesFollowing = mtrx[i][4];
    if ( nodesFollowing > 0) {
    int presStartIndex = startIndex;
    startIndex = 0;
    res += countLength(mtrx, rowcount, i, nodesFollowing) -2;
    startIndex = presStartIndex;
    }
    startIndex = i+1;
    }
    i++;
    }
    return res + 1;
    }

    int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
    int l = 0;
    int lt = 0;
    int j = 0;

    while (j < nodesFound) {

    lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
    if (lt > l)
    l = lt;
    j++;
    }
    return l;
    }

    int main(int argc, const char * argv[])
    {


    int m,k,i,j,s1,s2,res = 0,currlength;
    FILE *fp,*fp2;
    fp=fopen("scidinner.in","r");
    res = 0;


    fscanf(fp,"%d %d",&m,&k);



    int** mtrx;
    mtrx=(int**)malloc(k*sizeof(int*));
    for (i=0; i<k; i++)
    mtrx[i]=(int*)malloc(4*sizeof(int));



    i = 0;
    while(!feof(fp) && i < k)
    {
    fscanf(fp,"%d %d",&s1,&s2);

    mtrx[i][1] = s1;
    mtrx[i][2] = s2;
    mtrx[i][3] = -1;
    mtrx[i][4] = 0;

    i++;
    }
    fclose(fp);




    i=0;
    while (i < k) {
    s2 = mtrx[i][2];


    j=0;
    while (j < k) {
    if (mtrx[j][1] == s2){

    mtrx[j][3] = i;


    mtrx[i][4]++;
    }
    j++;
    }
    i++;
    }




    i=0;
    while (i < k) {
    if (mtrx[i][4] > 0){

    startIndex = 0;
    currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
    if (currlength > res)
    res = currlength;
    }
    i++;
    }


    fp2=fopen("scidinner.out","w");

    fprintf(fp2,"%d", res);
    fclose(fp2);





    for (i=0; i<k; i++)
    free((void*)mtrx[i]);
    free((void*)mtrx);

    return res;
    }




    But my debug is giving me these error reports:

    In function `main':
    [Linker error] undefined reference to `countLength'
    ld returned 1 exit status

    and I can't find my error...Please Heeelp!!!
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Code not formatted, rendering it unreadable. So I didn't bother to even try to read it. Nor will anybody else want to bother.

    This was your sixth post here, so you have been told this already: use code tags! Here is what it looks like when you do; compare this with your opening post and see the difference.

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #define _GNU_SOURCE
    
    int startIndex = 0;
    
    int countOneBranchLength(int** mtrx, int rowcount, int curRow){
        int res = 1;
        int i = startIndex;
           while (i < rowcount) {
            if (mtrx[i][1] == mtrx[curRow][2]){
                int nodesFollowing = mtrx[i][4];
                if ( nodesFollowing > 0) {
                    int presStartIndex = startIndex;
                    startIndex = 0;
                    res += countLength(mtrx, rowcount, i, nodesFollowing) -2;
                    startIndex = presStartIndex;
                }
                startIndex = i+1;
            }
            i++;
        }
        return res + 1;
    }
    
    int countLength(int** mtrx, int rowcount, int curRow, int nodesFound){
        int l = 0;
        int lt = 0;
        int j = 0;
        
        while (j < nodesFound) {
            
            lt = countOneBranchLength(mtrx,rowcount,curRow) + 1;
            if (lt > l)
                l = lt;
            j++;
        }
        return l;
    }
    
    int main(int argc, const char * argv[])
    {
        
        
        int m,k,i,j,s1,s2,res = 0,currlength;
        FILE *fp,*fp2;
        fp=fopen("scidinner.in","r");
        res = 0;
        
      
        fscanf(fp,"%d %d",&m,&k);
        
        
       
        int** mtrx;
        mtrx=(int**)malloc(k*sizeof(int*));
        for (i=0; i<k; i++)
            mtrx[i]=(int*)malloc(4*sizeof(int));
        
       
       
        i = 0;
        while(!feof(fp) && i < k)
        {
            fscanf(fp,"%d %d",&s1,&s2);
            
            mtrx[i][1] = s1;
            mtrx[i][2] = s2;
            mtrx[i][3] = -1;
            mtrx[i][4] = 0;
            
            i++;
        }
        fclose(fp);
       
        
        
    
        i=0;
        while (i < k) {
            s2 =  mtrx[i][2];
            
    
            j=0;
            while (j < k) {
                if (mtrx[j][1] == s2){
     
                    mtrx[j][3] = i;
                    
      
                    mtrx[i][4]++;
                }
                j++;
            }
            i++;
        }
        
    
        
    
        i=0;
        while (i < k) {
            if (mtrx[i][4] > 0){
      
                startIndex = 0;
                currlength = countLength(mtrx, k, i, mtrx[i][4]) ;
                if (currlength > res)
                    res = currlength;
            }
            i++;
        }
        
        
        fp2=fopen("scidinner.out","w");
    
        fprintf(fp2,"%d", res);
        fclose(fp2);
        
        
    
        
    
        for (i=0; i<k; i++)
            free((void*)mtrx[i]);
        free((void*)mtrx);
        
        return res;
    }
    In the first function, countOneBranchLength, you call another function, countLength, which the compiler doesn't know anything about. countLength shows up later, but that doesn't do any good. You need to add a function prototype for countLength that is placed above countOneBranchLength.

    Normally, this kind of mistake does not result in linker error, but rather in a "undefined reference" compiler warning.

    Also, if this a C++, then because of function overloading you may need to check that you always give it the right argument types.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Code:
    mtrx[i][1] = s1;
    mtrx[i][2] = s2;
    mtrx[i][3] = -1;
    mtrx[i][4] = 0;
    As far as I can tell, every single reference to the minor dimension of this matrix is 1-based rather than 0-based.

    So EVERY reference to mrtx[x][4] (really, what did you save by not saying matrix everywhere) is an out of bounds access.

    > int m,k,i,j,s1,s2,res = 0,currlength;
    Use meaningful variable names.
    i,j are fine, when they're just the loop control variables.
    But m,k - what are those?
    How about something like numRows and numCols.

    As well as being readable, it's a lot harder to make a mistake (and a lot easier to spot a mistake) than a scattering of single letter identifiers.

    A single letter variable takes only ONE key press to make the mistake, and the resulting bug is almost invisible to spot.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo