10

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.

TSG
  • 877
  • 1
  • 6
  • 23
  • I changed the compiler header library from Redlib to Newlib in the Project C/C++ build settings in eclipse. This takes care of the problem – TSG May 28 '15 at 20:24

4 Answers4

7

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
2

I add the same problem and I found out that using -std=gnu++11 compiler flag solves it.

stamm
  • 129
  • 1
  • 10
-1

The following may work for you (since strnlen() is not a part of the runtime lib).

Define your own/local version of the strnlen().

int strnlen(char *param, int maxlen)
{  
    // Perform appropriate string manipulation ... as needed.
    // Return what you need.
};  
artless noise
  • 21,212
  • 6
  • 68
  • 105
iammowgli
  • 159
  • 4
  • Why is using a 'strnlen' wrapper/user defined function frowned upon ... especially when he is stuck with a library, the source of which he doesn't want to modify, and with a platform, where the runtime library doesn't have strnlen defined ... ? – iammowgli May 13 '15 at 01:49
  • Thanks. In that case, he can simply have a second parameter 'maxlength' in his definition of strnlen() and perform the appropriate string manipulation within this function & return what he needs. My point being ... a user-defined wrapper could probably solve his problem. – iammowgli May 13 '15 at 20:48
  • By 'perform the appropriate string manipulation' in my earlier comment I was not implying usage of strlen() within the wrapper. What I meant was: 'write your own version of strnlen()' within this user defined wrapper ... – iammowgli May 14 '15 at 18:32
-3

You want this include instead:

#include <string.h>

The difference between <> and "" is that <> searches for header files in your systems include folder. The "" searches for header files in the current directory and in any other include folders specified by -I directory

John
  • 3,769
  • 6
  • 30
  • 49
  • 1
    I think #include "" and <> both search everywhere, and "" searches the current directory first and then the system header files, while <> follows the other order. (edit - that is incorrect) See (http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename) for better explainantion of the difference. Further, making that change does not solve the problem – TSG May 13 '15 at 00:26