What happened to strndup?

I am writing a parser using flex. I am using Mac OS X 10.6.7. I already have the header files:

#include "string.h"
#include "stdlib.h"

but he says

Undefined symbols for architecture x86_64:
  "_strndup", referenced from:
      _yylex in ccl2332A.o
ld: symbol(s) not found for architecture x86_64

why?

+3
source share
3 answers

AFAIK there is no strndup method in string.h or stdlib.h, try using strdup (), which you probably want. If you really need to specify the length you want to highlight, you can do this using malloc and memcpy.

+5
source

strndupis a GNU extension and is not present on Mac OS X. You will either have to not use it or supply some implementation, for example this one .

+2
source
char *strndup(char *str, int chars)
{
    char *buffer;
    int n;

    buffer = (char *) malloc(chars +1);
    if (buffer)
    {
        for (n = 0; ((n < chars) && (str[n] != 0)) ; n++) buffer[n] = str[n];
        buffer[n] = 0;
    }

    return buffer;
}
0
source

All Articles