简体   繁体   中英

undefined reference to avltree_init

In code provided by instructor there is:

typedef int (*avltree_cmp_fn_t)(const struct avltree_node *, const struct avltree_node *);

int avltree_init(struct avltree *tree, avltree_cmp_fn_t cmp, unsigned long flags);

After I define my_cmp function...

int my_cmp(const struct avltree_node *a, const struct avltree_node *b)
{
        struct my_struct *p = avltree_container_of(a, my_struct, node);
        struct my_struct *q = avltree_container_of(b, my_struct, node);
        return p->key - q->key;
}

and pass it as a parameter to avltree_init...

avltree_init(&tree, my_cmp, 0);

I get:

undefined reference to `avltree_init(avltree*, int (*)(avltree_node const*, avltree_node const*), unsigned long)'

Could someone explain, please, why it happens and where did I make a mistake? Thanks!

It seems from the error message that you're not linking with the file that contains the avltree_init function. You need to link all files, including the file containing that function, to your final application. This error comes from the linking phase, not from the compiling phase.

Furthermore, I see from your error message that you're using a C++ compiler for C code. You don't need to do that: it would be more optimal to use a C compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM