2015-10-13 74 views
1

我有以下文件:体系结构x86_64的未定义符号。如何正确包含?

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 

什么我错在这里做什么?它是否与我包含文件的方式有关?或者用我的汇编指令?我看到很多与我的标题相同的问题 - 但是,它们都不能帮助我解决我的错误。

+5

你需要编译这两个C文件。 – teppic

+0

将'extern'添加到头文件中的函数签名中。 –

回答

3

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

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

第一行错了 - 它会尝试链接一个可执行文件。 – teppic

+2

@teppic你是对的,修正了 –

+0

谢谢,编译都工作: gcc -o bst_test test_bst.c bst.c – ndrizza

相关问题