Undefined reference to 'strnlen' despite "string.h" include

TSG picture TSG · May 13, 2015 · Viewed 10.3k times · Source

I am trying to use create a project for LPC1769 on LPCXpresso. I have a C file calling

#include <string.h>
int main()
{
    //some stuff
    strnlen(SomeString, someInt);
}

to which I get an error:

Undefined reference to 'strnlen'

The weird part is that there is no problem with strcpy, strncpy or other common string functions.

I am building for a Cortex-M3 processor Compiler used is: arm-none-eabi-gcc In Eclipse, I have ticked the MCU linker option : No startup or default libs I am running Eclipse on Ubuntu

While it may be easy enough to bypass this by just using strlen, I am actually facing a problem using a library which uses strnlen, and I don't want to mess with the library source.

Answer

Thomas Dickey picture Thomas Dickey · May 13, 2015

The strnlen function was (until fairly recently) a Linux-specific function (some documentation such as the GNU libc manual still says that it is a "GNU extension"). The current manual page says it is part of POSIX.1-2008. Since you are cross-compiling, it is possible that the target machine's runtime library does not have this function. A forum posting from 2011 said just that.