Thread: undefined reference

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    undefined reference

    In function `main':

    : undefined reference to `binarySearch'

    I can't seem to figure out why the following is giving me an undefined reference:

    Code:
    int binarySearch(int numbers[],  int value, int size);
    
    int main()
    {
       int numbers[] =  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, };
        printf("Searching for -1 in numbers using binary search\n");
       binarySearch(numbers, -1, 15);
    
      return 0
    }
    
    
    int  binarysearch(int numbers[], int value, int size)
    {
       .... binary search code
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    C is case sensitive. You declare a function binarySearch, but define binarysearch.

  3. #3
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    thanks but hmm that worked for binary search but i have no idea why it isn't for linear search:

    Code:
    
    int linearSearch(int numbers[],  int value, int size);
    
    int main()
    {
    
         printf("\nSearching for 7 in numbers using linear search\n");
          linearSearch(numbers, 7, 15);
    
    }
    
    int linearSearch(int numbers[], int value, int size)
    {
       ... linearSearch code
    }
    i get the same error, undefined reference

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    undefined reference to what?

    It works for me. (assuming you've declared numbers somewhere?)

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well if that's your whole code, you should be getting undefined reference errors for not including stdio.h for printf() or for defining numbers[].

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    No, an "undefined reference" error occurs at link time, because it can't find the function, not because a prototype is missing.

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well then make that "undeclared"

  8. #8
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by cwr
    undefined reference to what?

    It works for me. (assuming you've declared numbers somewhere?)
    yes numbers is declared in main, and i've already imported stdio.h... strange

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Show us the exact code, and the exact error you get?

  10. #10
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    In function `main':
    : undefined reference to `linearSearch'
    collect2: ld returned 1 exit status

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well I just noticed you're passing an array to your function. You can't do that - you need to use a pointer.

  12. #12
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    i'll post the exact code in a sec, it's almost identical

  13. #13
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Quote Originally Posted by sean_mackrory
    Well I just noticed you're passing an array to your function. You can't do that - you need to use a pointer.
    are you sure? because the same prototype, and the function for binary search works fine.

  14. #14
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Code:
    #include <stdio.h>
    
    #define SIZE 15
    
    int binarySearch(int numbers[], int value, int size);
    int linearSearch(int numbers[], int value, int size);
    
    int main()
    {
       int numbers[] =  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    
       printf("Searching for -1 in numbers using linear search\n");
       linearSearch(numbers, -1, 15);
       printf("Searching for -1 in numbers using binary search\n");
       binarySearch(numbers, -1, 15);
    
       return 0;
    }
    
    int  binarySearch(int numbers[], int value, int size)
    { 
    }
    
    
    
    int linearSearch(int numbers[], int value, int size)
    {
    
    
    }
    
    }

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    edit: Yes - I'm mistaken - I'm confusing some techniques for strings with standards - but after trimming the ending brace on that code, it compiles fine for me. What compiler are you using?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM