Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C can't compile - symbol(s) not found for architecture x86_64

Tags:

c

compilation

Having a serious problem with my C code, I just don't seem to be able to get it to compile and I really can't figure out why.

I have tried researching online and can't find a solution to the problem, do you have any idea?

Thanks for your time!

Undefined symbols for architecture x86_64:
  "_Insert", referenced from:
      _InsertNode in part1.o
     (maybe you meant: _InsertNode)
  "_Create", referenced from:
      _findShortestPaths in part1.o
  "_DeleteMin", referenced from:
      _findShortestPaths in part1.o
  "_decreaseKey", referenced from:
      _findShortestPaths in part1.o
  "_GetMin", referenced from:
      _findShortestPaths in part1.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [part1] Error 1

Snippits from part1.c

#include "limits.h"
#include "pegBinaryHeap.h"

void InsertNode(int distance, Node* node, PriorityQueue PQ) {
  ...
  Insert(*item, PQ);
}

...

int* findShortestPaths(Graph *graph, int start) {
  ...

  //Priority queue ordered by distance
  PriorityQueue pq = Create(graph->MaxSize);
  for(int i = 0; i < graph->MaxSize; i++) {
    ...
  }

  //While the queue isn't empty:
  while((currentPqItem=GetMin(pq)) != NULL) { 
    ...
    DeleteMin(pq);

      //for each node accesable from currentNode
    List *currentNeighbour = currentNode.outlist;

    while(currentNeighbour!=NULL) {
      ...
        decreaseKey(currentNode.id, newDistance, pq);
    } // end for
  }// end while
}

int main(int argc,char *argv[])
{
  Graph mygraph;
  return 0;
}

And the .h file that it appears to be complaining about

#include "graph.h"

struct HeapStruct;
typedef struct HeapStruct *PriorityQueue;

typedef struct {
  int distance;
  Node *node;
} QueueType;

PriorityQueue Create( int MaxSize );
void Destroy( PriorityQueue H );
int Insert( QueueType Item, PriorityQueue H );
QueueType DeleteMin( PriorityQueue H );
QueueType* GetMin( PriorityQueue H );
void decreaseKey(int nodeId, int value, PriorityQueue H);
like image 881
Pez Cuckow Avatar asked Apr 13 '12 15:04

Pez Cuckow


2 Answers

You can compile, but you cannot link.

part1.o is using the functions you defined in your last .h file and the implementations cannot be found. When you link your program, you need to make sure you're linking in the object file(s) (or libraries) that contain the implementations of those functions. You've likely done something like:

gcc part1.c -o myapp

and so the linker doesn't have all the pieces to the puzzle.

If you want to compile in parts, you need to:

gcc -c part1.c -o part1.o
gcc -c implementations.c -o implementations.o 
gcc implementations.o part1.o -o myapp

Here, all the .c files are compiled into object (.o) files separately, and then linked together into an executable. Or you could do everything at once:

gcc part1.c implementations.c -o myapp

If the implementations are in a library (libimplementations.a):

gcc part1.c -Lpath/to/libs -limplementations -o myapp
like image 75
greg Avatar answered Nov 15 '22 09:11

greg


I would like to add I was getting a similar compilation error. See below.

Undefined symbols for architecture x86_64:
  "_add", referenced from:
   _load in file.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [all] Error 1

I solved this issue by implementing the add() functions prototype that I had created in my header file. Once implemented I did not receive this error anymore. May help someone else.

like image 5
NodeExplosion Avatar answered Nov 15 '22 11:11

NodeExplosion