strnlen不存在于gcc-4.2.1在Mac OS X 10.6.8 - 如何定义? [英] strnlen does not exist in gcc-4.2.1 on Mac OS X 10.6.8 - how to define it?

查看:248
本文介绍了strnlen不存在于gcc-4.2.1在Mac OS X 10.6.8 - 如何定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个跨平台的OS X版本的最新 dcraw.c 我这样做在OS X 10.6.8具有PPC兼容性。现在我的问题是strnlen似乎在最新版本的程序中使用,它不存在于10.6.8和gcc给我这样的消息:

 架构i386的未定义符号:
_strnlen,引用自:
...
架构的未定义符号ppc:
_strnlen引用自:
...

所以,我想定义strnlen,

>

我的gcc编译命令是这个btw:

  gcc -o dcraw -O4 -Wall -force_cpusubtype_ALL -mmacosx-version-min = 10.4 -arch i386 -arch ppc dcraw.c -lm -DNODEPS 


解决方案

strnlen 是一个GNU扩展,也在POSIX(IEEE Std 1003.1-2008)中指定。如果 strnlen 不可用(从10.7开始),请使用以下替换。

  //如果strnlen丢失,请使用此方法。 
size_t strnlen(const char * str,size_t max)
{
const char * end = memchr(str,0,max);
return end? (size_t)(end-str):max;
}


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

解决方案

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;
}

这篇关于strnlen不存在于gcc-4.2.1在Mac OS X 10.6.8 - 如何定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆