使用talloc时编译错误

pre*_*uin 2 c memory compiler-errors

我编写下面的代码来测试talloc:

#include "talloc.h"

typedef struct linklist
{
    char* str;
    struct linklist* next;
}LinkList;

int main(int argc,char* argv[])
{
    LinkList* lptr=talloc(NULL,LinkList);
    lptr->str=talloc_strdup(lptr,"Test ptr");

    talloc_free(lptr);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但得到了编译错误:

talloctest.c:(.text+0x21): undefined reference to `talloc_named_const(void const*, unsigned int, char const*)'
talloctest.c:(.text+0x39): undefined reference to `talloc_strdup(void const*, char const*)'
talloctest.c:(.text+0x4d): undefined reference to `talloc_free(void*)'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

然后我找到相关宏和函数的定义:"talloc.h"中的定义:

#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
char *talloc_strdup(const void *t, const char *p);
Run Code Online (Sandbox Code Playgroud)

"talloc.c"中的实现:

void *talloc_named_const(const void *context, size_t size, const char *name)
{
    return _talloc_named_const(context, size, name);
}

char *talloc_strdup(const void *t, const char *p)
{
    if (unlikely(!p)) return NULL;
    return __talloc_strlendup(t, p, strlen(p));
}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我为什么吗?我已经尝试了talloc-1.3.0和talloc-2.0.7,但得到了相同的结果.

sim*_*onc 5

您是否将talloc用作单独的库或将其编译为可执行文件?如果您将它作为一个单独的库使用,则需要链接到它 - 通过添加-ltalloc到您的构建命令来猜测.