未定义的体系结构符号 x86_64。如何正确包含?

Undefined symbols for architecture x86_64. How to include correctly?

我有以下文件:

bst.h:只包含声明

typedef struct Node Node;
struct Node {
    unsigned int key;
    Node *left;
    Node *right;
};

int insert(Node *node);
Node* lookup(unsigned int key);
int delete(Node *node);

bst.c:定义声明

#include "bst.h"

#include <stdio.h>


Node *root = NULL;

int insert(Node* node) {
    ... implementation ...
    return 0;
}

Node* lookup(unsigned int key) {
    ... implementation ...
    return current;
}

int delete(Node *node) {
    ... implementation ...
    return 0;
}

test_bst.c:测试 BST 实现

#include "bst.h"
#include <stdio.h>

int main() {

    Node node = {10,NULL,NULL};

    insert(&node);

    return 0;
}

如果我 运行 gcc test_bst.c 我得到以下错误:

Undefined symbols for architecture x86_64:
  "_insert", referenced from:
      _main in cc1m0mA1.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status

我在这里做错了什么?它与我包含文件的方式有关吗?还是按照我的编译说明?我看到了很多与我的标题相同的问题 - 然而,none 对解决我的错误很有帮助。

您没有包含实际实现 insert 函数的文件。你可以这样做:

gcc -c -o bst.o bst.c
gcc -o test test_bst.c bst.o