"Undefined Symbols" error when compiling project

Asked

Viewed 79 times

0

I am trying to compile a project in C++, but it is 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 that might be?

I am using the Macos X 10.9 system. Follow the code section 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);
}
  • Did you link the lib when compiling? Maybe the link is on the wrong track, I assume that’s what this message means "Linker command failed"

1 answer

1

The source file where the "knapsack" function was set was not included in the build.

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

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

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

g++ -o xxx main.cpp knapSack.cpp
  • I declared the "knapsack" function on a . h file, which is included at the beginning of the code. Not only that to work, in any case function calls that are in other files?

  • It is not enough just to statement a function or variable, it is also necessary to definition. Note that in C++ these are technical terms. You need to compile the source file where the knapsack function is definite, and include the object file (.o or .obj) generated in the link-edit. If it is a function provided by a library, you need to include the library in the link-edit. How are you compiling ? Is it on the command line ? Is it with IDE? You need to give more information to have a more specific answer.

  • I am compiling on the command line, with g++. I think I understand what you said. I was able to include the files correctly now. But to be clear, I created a . h with the prototype (the statement, as you said) of the knapsack function and a . cpp for its implementation (the definition). In my main, which calls the function, I must call . cpp?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.