C无法编译 - 找不到架构x86_64的符号

Pez*_*kow 14 c compilation

我的C代码存在严重问题,我似乎无法将其编译,我真的无法弄清楚为什么.

我尝试过在线研究,找不到问题的解决方案,你有什么想法吗?

谢谢你的时间!

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
Run Code Online (Sandbox Code Playgroud)

来自part1.c的Snippits

#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;
}
Run Code Online (Sandbox Code Playgroud)

而.h文件似乎在抱怨

#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);
Run Code Online (Sandbox Code Playgroud)

gre*_*reg 23

你可以编译,但你无法链接.

part1.o正在使用您在上一个.h文件中定义的函数,但无法找到实现.链接程序时,需要确保链接包含这些函数实现的目标文件(或库).你可能做过这样的事情:

gcc part1.c -o myapp

所以链接器没有拼图的所有部分.

如果要编译部分,则需要:

gcc -c part1.c -o part1.o
gcc -c implementations.c -o implementations.o 
gcc implementations.o part1.o -o myapp
Run Code Online (Sandbox Code Playgroud)

这里,所有.c文件分别编译成object(.o)文件,然后链接到一个可执行文件中.或者你可以一次完成所有事情:

gcc part1.c implementations.c -o myapp
Run Code Online (Sandbox Code Playgroud)

如果实现在库(libimplementations.a)中:

gcc part1.c -Lpath/to/libs -limplementations -o myapp
Run Code Online (Sandbox Code Playgroud)


小智 5

我想补充一点,我得到了类似的编译错误.见下文.

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
Run Code Online (Sandbox Code Playgroud)

我通过实现我在头文件中创建的add()函数原型解决了这个问题.一旦实现,我就不再收到此错误.可以帮助别人.