I write the code below to test the 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;
}
But got compile error:
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
Then I find the definition of relative macros and functions:
Definition in “talloc.h”:
#define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
char *talloc_strdup(const void *t, const char *p);
Implemetation in “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));
}
Can someone tell me why?I have tried both talloc-1.3.0 and talloc-2.0.7,but got the same result.
Are you using talloc as a separate library or compiling it into your executable? If you’re using it as a separate library, you’ll need to link to it – at a guess by adding
-ltallocto your build command.