Thread: What does it mean?

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    99

    What does it mean?

    When compiling I receive an error from gcc:
    Undefined first referenced
    symbol in file
    project1 /var/tmp//ccAmPHuD.o
    ld: fatal: Symbol referencing errors. No output written to a.out
    collect2: ld returned 1 exit status
    How do I find out what my mistake is???

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    I couldn't make much sense of most of that error message, but usually the compiler will tell you on what line number of the file the error occurs on, so that narrows down where you have to look. And the message itself should give you a clue. "Undefined reference" usually just means you're using a variable or instance of a class that hasn't actually been declared or something similar. So check for that. If you can't figure it out yourself, post your code (be sure to use code tags and proper code formatting - see the sticky at the top of each forum), and see if the more experienced members can help you pinpoint the problem.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Most likely a typo in your source code, assuming you actually wrote any. As you didn't post any, I can only assume that you did?
    Check function names for typing error/mis-spellings
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    This is the first time I am using a library function with "..._t" so I can only guess I might not have wrote the whole thing properly.
    The code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/time.h>
     
    hrtime_t gethrtime(void);
    int recursive_fibonacci(int);
    int iterative_fibonacci(int);
     
    int main()
    {
     
       int n;
       hrtime_t start, end;
       int result;
     
       printf("Enter n for which to test the average time of running: ");   
       scanf("%d", &n);
     
     
       start = gethrtime();
       result = recursive_fibonacci(n);
       end = gethrtime();
       printf("The average time for a recursive funtion was: %lld nsec.\n", (end - start)/n);
     
       start = gethrtime();
       result = iterative_fibonacci(n);
       end = gethrtime();
       printf("The average time for an iterative function was: %lld nsec.\n", (end - start)/n);
     
       printf("The Fibonacci number was: %d\n", result);
     
       return 0;
    }
     
    int recursive_fibonacci(int n)
    {
       if (n == 0) 
          return 0;
       else if (n == 1) 
          return 1;
       else
          return (fibonacci(n-1) + fibonacci(n-2)); 
    }
     
    int iterative_fibonacci(int n)
    {
       int i;
       int next_fib;
       int first_fib = 0;
       int second_fib = 1;
       
       for(i = 2; i < n; i++)
          {
          next_fib = first_fib + second_fib;
          first_fib = second_fib;
          second_fib = next_fib;
          }
       return next_fib;
    }
    I already looked for a typo, but I can't see any.
    And the error given by the compiler was pasted in full. No line reference at all...
    Can you tell me what happened here?

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I don't see how the error could have been made but:

    Code:
    return (fibonacci(n-1) + fibonacci(n-2));
    That being in your recursive_fibonacci function. I don't think fibonacci(..) exists, and that you meant to call it recursively. But that's still an odd error to come from that, so I don't think that's the source of the problem.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    hrtime_t gethrtime(void);
    I dont see the implementation for that function.

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    I wrote this program exactly as the example showed me, I did not know there was supposed to be something else there?

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks Tonto, this is exactly what the problem was! Now it works.

Popular pages Recent additions subscribe to a feed