免费注册 查看新帖 |

Chinaunix

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

为什么编译出错 ? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-04-17 19:24 |只看该作者 |倒序浏览
#include <pthread.h>
#include "errors.h"

typedef struct tsd_tag{
    pthread_t thread_id ;
    char *string ;
}tsd_t ;

pthread_key_t tsd_key ;
pthread_once_t key_once = PTHREAD_ONCE_INIT ;

void once_routine(void)
{
    int status ;
    printf("initializing key\n") ;
    status = pthread_key_create(&tsd_key,NULL) ;
    if(status != 0)
        err_abort(status,"Create key") ;
        
}

void *thread_routine(void *arg)
{
    tsd_t *value ;
    int status ;
   
    status = pthread_once(&key_once,once_routine) ;
    if(status != 0)
        err_abort(status,"once_init") ;
    value = (tsd_t*)malloc(sizeof(tsd_t)) ;
    if(value ==NULL)
        errno_abort("allocate key value") ;
    status = pthread_setspecific(tsd_key,value) ;
    if(status != 0)
        err_abort(status,"set tsd") ;
    printf("%s set tsd value %p \n",arg,value) ;
    value->thread_id = pthread_self() ;
    value->string = (char *)arg ;
    value = (tsd_t*)pthread_getspecific(tsd_key) ;
    printf("%s starting...\n",value->string) ;
    sleep(2) ;
    value = (tsd_t*)pthread_getspecific(tsd_key) ;
    printf("%s done...\n",value->string) ;
    return NULL ;
   
}

void main(int argc,char *argv[])
{
   pthread_t thread1,thread2 ;
   int status ;
   status = pthread_create(&thread1,NULL,thread_routine,"thread1") ;
   if(status != 0)
       err_abort(status,"Create thread1") ;
   status = pthread_create(&thread2,NULL,thread_routine,"thread2") ;
   if(status != 0)
       err_abort(status,"Creat thread2") ;
   pthread_exit(NULL) ;
}

论坛徽章:
0
2 [报告]
发表于 2007-04-17 19:33 |只看该作者
[root@localhost tsd_once]# gcc tsd_once.c -o tsd_once
tsd_once.c: In function `main':
tsd_once.c:49: warning: return type of `main' is not `int'
/tmp/ccDl90rA.o(.text+0x21): In function `once_routine':
: undefined reference to `pthread_key_create'
/tmp/ccDl90rA.o(.text+0x7e): In function `thread_routine':
: undefined reference to `pthread_once'
/tmp/ccDl90rA.o(.text+0x127): In function `thread_routine':
: undefined reference to `pthread_setspecific'
/tmp/ccDl90rA.o(.text+0x1a0): In function `thread_routine':
: undefined reference to `pthread_getspecific'
/tmp/ccDl90rA.o(.text+0x1d7): In function `thread_routine':
: undefined reference to `pthread_getspecific'
/tmp/ccDl90rA.o(.text+0x222): In function `main':
: undefined reference to `pthread_create'
/tmp/ccDl90rA.o(.text+0x279): In function `main':
: undefined reference to `pthread_create'
collect2: ld returned 1 exit status

论坛徽章:
208
巨蟹座
日期:2013-09-02 09:16:36卯兔
日期:2013-09-02 20:53:59酉鸡
日期:2013-09-05 21:21:45戌狗
日期:2013-10-15 20:51:17寅虎
日期:2013-10-18 21:13:16白羊座
日期:2013-10-23 21:15:19午马
日期:2013-10-25 21:22:48技术图书徽章
日期:2013-11-01 09:11:32双鱼座
日期:2013-11-01 20:29:44丑牛
日期:2013-11-01 20:40:00卯兔
日期:2013-11-11 09:21:32酉鸡
日期:2013-12-04 19:56:39
3 [报告]
发表于 2007-04-17 20:00 |只看该作者
need -lpthread

论坛徽章:
0
4 [报告]
发表于 2007-04-17 22:17 |只看该作者
果然要加-lpthread,谢谢,可是还是有错误
[root@localhost tsd_once]# gcc -lpthread tsd_once.c -o tsd_once
tsd_once.c: In function `main':
tsd_once.c:49: warning: return type of `main' is not `int'

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
5 [报告]
发表于 2007-04-17 23:20 |只看该作者
原帖由 springzbz 于 2007-4-17 22:17 发表于 4楼  
果然要加-lpthread,谢谢,可是还是有错误
# gcc -lpthread tsd_once.c -o tsd_once
tsd_once.c: In function `main':
tsd_once.c:49: warning: return type of `main' is not `int'


看看这个帖子:
[转贴]为什么在类 Unix 系统下 "void main(void)" 是错误的

或者原文
void main(void) - the Wrong Thing

[ 本帖最后由 MMMIX 于 2007-4-18 14:50 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP