我有以下文件:
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
我在这里做错了什么?它与我包含文件的方式有关吗?还是按照我的编译说明?我看到了很多与我的标题相同的问题 - 但是,它们都没有帮助解决我的错误。
最佳答案
您没有包含实际实现 insert
函数的文件。你可以这样做:
gcc -c -o bst.o bst.c
gcc -o test test_bst.c bst.o
关于c - 体系结构 x86_64 的 undefined symbol 。如何正确包含?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33109527/