Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strnlen does not exist in gcc-4.2.1 on Mac OS X 10.6.8 - how to define it?

Tags:

c++

c

macos

gcc

I'm building a cross platform OS X version of the latest dcraw.c I'm doing this on OS X 10.6.8 to have the PPC compatibility. Now my problem is that strnlen seems to get used in the latest version of the program and it does not exist on 10.6.8 and gcc gives me messages like this:

Undefined symbols for architecture i386:
  "_strnlen", referenced from:
...
Undefined symbols for architecture ppc:
  "_strnlen", referenced from:
...

So, I'd like to just define strnlen but don't quite know how.

Q: Can anyone please provide a working definition of strnlen to use in dcraw.c?

My gcc compilation command is this btw:

gcc -o dcraw -O4 -Wall -force_cpusubtype_ALL -mmacosx-version-min=10.4 -arch i386 -arch ppc dcraw.c -lm -DNODEPS
like image 458
C.O. Avatar asked Sep 08 '15 22:09

C.O.


1 Answers

strnlen is a GNU extension and also specified in POSIX (IEEE Std 1003.1-2008). If strnlen is not available (it is since 10.7) use the following replacement.

// Use this if strnlen is missing.
size_t strnlen(const char *str, size_t max)
{
    const char *end = memchr (str, 0, max);
    return end ? (size_t)(end - str) : max;
}
like image 150
HelloWorld Avatar answered Oct 01 '22 13:10

HelloWorld