Chinaunix

标题: 线程头文件问题 [打印本页]

作者: feiyinziiu    时间: 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 编辑 ]
作者: T-Bagwell    时间: 2009-11-18 22:50
编译的时候是不是加个-lpthread -pthread一类的呢?
作者: feiyinziiu    时间: 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()函数之外申请的互斥锁,在其他的几个线程函数中,也报错说我没定义。。
作者: feiyinziiu    时间: 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 }
作者: feiyinziiu    时间: 2009-11-19 00:11
我啥也不说了。
唉。大家可以不用回答此问题了。
都是当时老师讲谭的C程序设计的时候全局变量没怎么听。
刚刚翻看谭哥的书一看。原来如此。
extern




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2