"Undefined symbols" error while compiling project

0

I'm trying to compile a project in C ++, but it's giving the following error:

Undefined symbols for architecture x86_64:

  "knapSack(int, int*, int*, int)", referenced from:

      _main in main-b722ae.o

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any idea what this might be?

I'm using the MacOs X 10.9 system. Here is the snippet of the code where the error is being pointed out:

// Returns the maximum value that can be put in a knapsack of capacity W

int knapSack(int W, int wt[], int val[], int n)
{
   int i, w, profit, cont = 0;

   item* K = NULL;

   //Build table K[][] in bottom up manner

   for (i = 0; i <= n; i++)    
   {    
       for (w = 0; w <= W; w++)   
       {    
           if (i==0 || w==0) 
           {
               profit = 0;

               K = insertItem(profit, K);
           }

           else if (wt[i-1] <= w)
           { 
               profit = max(val[i-1] + returnItem(K, (i-1), (w-wt[i-1]), W),  returnItem(K, (i-1), w, W));

               K = insertItem(profit, K);
           }

           else  
           {
               profit = returnItem(K, (i-1), w, W);

               K = returnItem(profit, K); 
           }

           cont++;

            if(cont % 1000 == 0)
            {

                cout << "i = "<< i << "w = " << w << "profit = " << profit << endl;//printf("i = %d, w = %d, profit = %d \n", i, w, profit);

            }
       }
   } 
   return returnItem(K, (i-1), (w-1), W);
}
    
asked by anonymous 23.09.2016 / 18:59

1 answer

1

The source file where the "knapSack" function was defined was not included in the build.

Or, if it is a function provided by a library, this library should be provided in the link-editing.

How to do it? This depends on how you are working, whether it is command line or whether it is an IDE.

Assuming command line and gcc, and assuming you have the knapSack.h and knapSack.cpp files with the interface and the implementation of the function, then you need to include the knapSack.cpp file in the build, for example:

g++ -o xxx main.cpp knapSack.cpp
    
23.09.2016 / 19:06