免费注册 查看新帖 |

Chinaunix

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

线程头文件问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-18 21:50 |只看该作者 |倒序浏览

------------------------------------------------------------
pthrad1.h
------------------------------------------------------------
#
#
#

extern void pthread1(void);



-------------------------------------------
pthread.c
--------------------------------------------

void pthrad1(void)
{
    ....
    ....
}


------------------------------------------
main.c
-------------------------------------------
#include  "pthread1.h"
int main(int argc, char **argv)
{
      .....
       pthread = pthread_create(&id1, NULL, (void *)pthread1,NULL);
}





编译的时候竟然告诉我     pthraed1没定义。后来在main中exern pthread1();之后就好了。

经常是在main中写函数的 实现。最经发现很多高手都是一个线程用一套.h和.c文件搞定。然后在main函数中create线程。
但是今天自己写的时候却出现问题。不仅是函数说没找到。
我在main.c文件中定义的全局变量编译时在pthread.c中也报错说没定义。

请各位指点。谢谢

[ 本帖最后由 feiyinziiu 于 2009-11-18 21:51 编辑 ]

论坛徽章:
5
摩羯座
日期:2014-07-22 09:03:552015元宵节徽章
日期:2015-03-06 15:50:392015亚冠之大阪钢巴
日期:2015-06-12 16:01:352015年中国系统架构师大会
日期:2015-06-29 16:11:2815-16赛季CBA联赛之四川
日期:2018-12-17 14:10:21
2 [报告]
发表于 2009-11-18 22:50 |只看该作者
编译的时候是不是加个-lpthread -pthread一类的呢?

论坛徽章:
0
3 [报告]
发表于 2009-11-18 22:59 |只看该作者

回复 #2 T-Bagwell 的帖子

加了  -lpthread.

斑竹,我想问下。我有3个线程。分别每个线程都有 .h  .c文件组成。
然后再

main.c  #include  "  .h"
然后再在main()函数之外申请的只要不是static变量,我定义其他的变量,那么在我的那些线程.c文件中使用都没问题把?

我自己写的Makefile如下。

EXEC = main
OBJS = main.o water_pthread.o time_pthread.o
SRC = main.c time_pthread.c water_pthread.c

CC = gcc

$(EXEC) : $(OBJS)
         $(CC) -o $(EXEC)  $(OBJS)  -lpthread
main.o:time_pthread.h  water_pthread.h


clean:
         rm -rif *.o


我在main.c 里。main()函数之外申请的互斥锁,在其他的几个线程函数中,也报错说我没定义。。

论坛徽章:
0
4 [报告]
发表于 2009-11-18 23:03 |只看该作者
main.c函数如下:
#include "time_pthread.h"
  2 #include "water_pthread.h"
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #include <unistd.h>
  6 #include <sys/types.h>
  7 #include <pthread.h>
  8
  9
10 pthread_cond_t g_collect_info = PTHREAD_COND_INITIALIZER;
11 pthread_mutex_t g_condition_mutex = PTHREAD_MUTEX_INITIALIZER;
12
13 int main(int argc, char **argv)
14 {
15         pthread_t id1, id2, id3;
16         int ret1, ret2, ret3;
17 //      extern void water_pthread(void);
18
19 //      pthread_cond_t g_collect_info = PTHREAD_COND_INITIALIZER;
20 //      pthread_mutex_t g_condition_mutex = PTHREAD_MUTEX_INITIALIZER;         
21
22         ret1 = pthread_create(&id1, NULL, (void *)time_pthread, NULL);
23         ret2 = pthread_create(&id2, NULL, (void *)water_pthread, NULL);
24
25         pthread_join(id1, NULL);
26         pthread_join(id2, NULL);
27         return 0;
28 }

论坛徽章:
0
5 [报告]
发表于 2009-11-19 00:11 |只看该作者
我啥也不说了。
唉。大家可以不用回答此问题了。
都是当时老师讲谭的C程序设计的时候全局变量没怎么听。
刚刚翻看谭哥的书一看。原来如此。
extern
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP