UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C
  • » Undefined first referenced symbol in file

#1 2007-04-25 07:32 AM

aaronc
Member
Registered: 2007-04-25
Posts: 8

Re: Undefined first referenced symbol in file

Hi all, I am trying to compile the following simple program.

#include <stdio.h>

#define SIZE 16

void binarySearch(int numbers[], int value);
void linearSearch(int numbers[], int value);

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);
   printf("Searching for -1 in numbers using binary search\n");
   binarySearch(numbers, -1);

   printf("\nSearching for 0 in numbers using linear search\n");
   linearSearch(numbers, 0);
   printf("Searching for 0 in numbers using binary search\n");
   binarySearch(numbers, 0);

   printf("\nSearching for 7 in numbers using linear search\n");
   linearSearch(numbers, 7);
   printf("Searching for 7 in numbers using binary search\n");
   binarySearch(numbers, 7);

   printf("\nSearching for 11 in numbers using linear search\n");
   linearSearch(numbers, 11);
   printf("Searching for 11 in numbers using binary search\n");
   binarySearch(numbers, 11);

   printf("\nSearching for 15 in numbers using linear search\n");
   linearSearch(numbers, 15);
   printf("Searching for 15 in numbers using binary search\n");
   binarySearch(numbers, 15);

   printf("\nSearching for 22 in numbers using linear search\n");
   linearSearch(numbers, 22);
   printf("Searching for 22 in numbers using binary search\n");
   binarySearch(numbers, 22);

   return 0;
}

void binarysearch(int numbers[], int value)
{
   int step = 1, left = 0, right = SIZE-1, mid;

   while (left <= right)
   {
      mid = (left + right) / 2;
      if (numbers[mid] == value)
      {
         printf("Number found after %d steps\n", step);
         return;
      }

      
      else if (numbers[mid] < value)
         left = mid+1;      
      else
         right = mid-1;
   
      step++;   
   }
   printf("Number not found after %d steps\n", step);
}

void linearSearch(int numbers[], int value)
{
   int i;

   for (i=0; i < SIZE; i++)
   {
     if (numbers[i] == value)
     {
        printf("Number found");
        return;
     }
   }

   printf("Number not found");


   /* Use linear search to find value in numbers.
      Print the number of steps it takes to find or not find value
   */  

}

but when I  compile it using gcc, I get the error below..

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


please some1 help me, I am using linux with putty software.

Offline

#2 2007-04-25 08:05 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: Undefined first referenced symbol in file

Be more attentive, please!
Almost 10 minutes spent to find out that typo : no implementation for binarySearch function, as gcc claims ;)

Offline

#3 2007-04-25 12:15 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Undefined first referenced symbol in file

Hint: It's all case sensitive.

Offline

#4 2007-04-25 01:00 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Undefined first referenced symbol in file

void binarySearch(int numbers[], int value);
...
void binarysearch(int numbers[], int value)

Offline

#5 2007-04-29 04:07 PM

aaronc
Member
Registered: 2007-04-25
Posts: 8

Re: Undefined first referenced symbol in file

HI,

Thanks every1 for the help, I know it was silly that I forgot the case sensitive thing. Sorry for replying so late as I had been really busy. Thanks again..

aaron

Offline

  • Index
  • » C
  • » Undefined first referenced symbol in file

Board footer

Powered by FluxBB