c - 体系结构 x86_64 的 undefined symbol 。如何正确包含?

标签 c include

我有以下文件:

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/

相关文章:

c - 无法计算输出中的行数

多处理器系统上的临界区和内存栅栏/屏障

c - 记事本无法识别换行符 '\n',但可以与制表符等其他特殊字符配合使用

include - 在主机文件中使用包含内容

C 包含路径 : Safe to use hyphen & underscore?

objective-c - #include 或#import <objc/runtime.h>?

c - Variadic 函数没有正确传递第一个参数

c - 有没有办法在 AVR gcc 中为引脚定义宏,以便我可以将它们作为变量访问?

具有 GET 属性的 PHP include() (include file.php?q=1)

c++ - 是否缺少必需的包含未定义行为?