免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 6443 | 回复: 13
打印 上一主题 下一主题

[函数] hp-ux的localtime_r等函数问题? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-10-09 16:04 |只看该作者 |倒序浏览
-bash-2.05b$ g++ -v
Reading specs from /usr/local/lib/gcc/hppa2.0w-hp-hpux11.11/3.4.3/specs
Configured with: ./configure  : (reconfigured) ./configure  : (reconfigured) ./configure  : (reconfigured) ./configure  : (reconfigured) ./configure  : (reconfigured) ./configure
Thread model: single
gcc version 3.4.3


一个cpp文件中包含这样的代码:


  1. #include <time.h>
  2.     time_t tt;
  3.     struct tm ut;
  4. ...
  5.     tt = time(NULL);
  6. ...
  7.     if (localtime_r(&tt, &ut) == NULL)
  8.     {
  9. ...
  10.     }

复制代码


想移植到hp上去,但是编译出问题了:
til.cpp:2939: error: `localtime_r' undeclared (first use this function)



  1. -bash-2.05b$ man localtime_r
  2. ..
  3.       #include <time.h>
  4.       struct tm *localtime_r(const time_t *timer, struct tm *result);

复制代码


为啥呢?hp-ux好像很变态,尤其是这几个函数:em12:

论坛徽章:
0
2 [报告]
发表于 2006-10-09 16:36 |只看该作者
很多不可重入(非线程安全)的函数提供另外一个同名的可重入版本函数.
不可重入的函数原型大多是:
char *function(argv[]) ;
对应的可重入的函数原型大多是:
int function_r(char *retResult, ... /*.. original arguments */ );

仔细追究源代码,一般在不可重入的函数中都有:

  1. static char buf[...MAX];
  2. ...
  3. return buf;
复制代码


正是因为这个返回结果的静态定义导致了该函数的不可重入(原因大家都很清楚).

而提供的可重入函数,其实质是让用户来定义存放结果的存储空间(一般采用动态分配,传入的是你想要保存结果的存储空间的地址)

对于楼主的问题,其实应该是非常容易理解的.
HP-UX已经很多年没有Roadmap了,他所提供给你的库自然是以前开发的现成的库,其它平台提供的线程安全的函数在HP平台上找不到也是再正常不过的了.

论坛徽章:
0
3 [报告]
发表于 2006-10-09 16:55 |只看该作者
谢谢! 可是那个man也是在hp-ux上找到的呀

论坛徽章:
0
4 [报告]
发表于 2006-10-09 17:16 |只看该作者
找到了别人代码中的一段
解决了



  1. 00035 /* the following two preprocessor defines are to include the prototypes for
  2. 00036  * gmtime_r() and asctime_r() from /usr/include/time.h
  3. 00037  * HOWEVER, they do no good if -ansi is used in gcc: warnings are generated
  4. 00038  * that the prototypes have not been seen */
  5. 00039
  6. 00040 /* HP-UX and Solaris */
  7. 00041 #ifndef _REENTRANT
  8. 00042 #   define _REENTRANT
  9. 00043 #endif
  10. 00044
  11. 00045 #ifndef _POSIX_PTHREAD_SEMANTICS
  12. 00046 #   define _POSIX_PTHREAD_SEMANTICS
  13. 00047 #endif

复制代码

论坛徽章:
0
5 [报告]
发表于 2006-10-09 17:22 |只看该作者
原帖由 dajun 于 2006-10-9 16:55 发表
谢谢! 可是那个man也是在hp-ux上找到的呀

localtime_r' undeclared (first use this function)

没有定义,也就是没有找到,把你的源代码给大家看看,一起分析和学习啊

[ 本帖最后由 wuqing 于 2006-10-9 17:26 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2006-10-09 17:26 |只看该作者
最简单的测试:
# uname
HP-UX
# uname -a
HP-UX abcde B.11.23 U 9000/800 2413402663 unlimited-user license

# more timetest.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>

  4. main()
  5. {
  6.         time_t *m_time;
  7.         struct tm *m_tm;
  8.         
  9.         m_time = mktime(NULL);
  10.         localtime_r(m_time, m_tm);

  11. }
复制代码

# cc -o timetest timetest.c
# ./timetest

好分特,HP竟然不支持ANSI C?

论坛徽章:
0
7 [报告]
发表于 2006-10-09 17:29 |只看该作者
关于HP不支持ANSI函数原型的证据如下:
# cat > test.c

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(int argc, char **argv)
  4. {
  5.         printf("Hello world!\n");
  6.         return(0);
  7. }
复制代码

# cc -o test test.c
(Bundled) cc: "test.c", line 3: error 1705: Function prototypes are an ANSI feature.

论坛徽章:
0
8 [报告]
发表于 2006-10-09 17:29 |只看该作者
Thread model: single
单线程

论坛徽章:
0
9 [报告]
发表于 2006-10-09 17:34 |只看该作者
我的机器倒是可以:

  1. -bash-2.05b$ cat >1.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(int argc, char **argv)
  5. {
  6.         printf("Hello world!\n");
  7.         return(0);
  8. }


  9. -bash-2.05b$ cc -o test 1.c
  10. -bash-2.05b$ cc -v
  11. cc: informational note 404: NLSPATH is /opt/ansic/lib/nls/msg/%L/%N.cat:/opt/ansic/lib/nls/msg/C/%N.cat:
  12. cc: informational note 404: INCLUDIR is INCLUDIR=/usr/include
  13. cc: informational note 404: LPATH is /usr/lib:/opt/langtools/lib:
  14. /usr/ccs/bin/ld /opt/langtools/lib/crt0.o -u main -lc
  15. cc: informational note 413: Entering Link editor.
  16. /usr/ccs/bin/ld: Unsatisfied symbols:
  17.    main (Not referenced yet! Probably due to -u option)
  18. -bash-2.05b$ ls -al `which cc`
  19. -r-xr-xr-x   1 bin        bin         614400 Mar  5  2006 /usr/bin/cc
  20. -bash-2.05b$
复制代码

论坛徽章:
0
10 [报告]
发表于 2006-10-09 17:36 |只看该作者
原帖由 wuqing 于 2006-10-9 17:22 发表

localtime_r' undeclared (first use this function)

没有定义,也就是没有找到,把你的源代码给大家看看,一起分析和学习啊


大概是因为是c++的原因,返回值不同,他认为不存在了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP