пожалуйста помогите в составлении этой программы - PullRequest
0 голосов
/ 10 ноября 2010
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>

void *WriteNumbers(void *threadArg)
{
 int start, stop;
 start = atoi((char *)threadArg);
 stop = start + 10;

 while(start<stop)
 {
  printf("%d\n", start++);
  sleep(1);
 }
}

int main(int argc, char **argv)
{
 pthread_t thread1, thread2;

 // create the threads and start the printing 
 pthread_create(&thread1, NULL, WriteNumbers, (void *)argv[1] );
 pthread_create(&thread2, NULL, WriteNumbers, (void *)argv[2]);

 pthread_join(thread1, NULL);
 pthread_join(thread2, NULL);

 return 0;
}

gcc -o pthread pthread.c 
/tmp/cckJD3rd.o: In function `main':
pthread.c:(.text+0x7a): undefined reference to `pthread_create'
pthread.c:(.text+0xa2): undefined reference to `pthread_create'
pthread.c:(.text+0xb6): undefined reference to `pthread_join'
pthread.c:(.text+0xca): undefined reference to `pthread_join'
collect2: ld returned 1 exit status

Ответы [ 5 ]

8 голосов
/ 10 ноября 2010

Вам необходимо добавить флаг -pthread в ваш компилятор.

5 голосов
/ 10 ноября 2010

Вы не включили библиотеку pthread.Попробуйте добавить

-lpthread 

в строку с комплиментами.

1 голос
/ 10 ноября 2010

Вы не связываетесь с libpthread. И ручная компиляция с помощью gcc - это действительно BFI. Поместите свой код в файл с именем 'thread_test.c', а затем выполните:

make thread_test LDFLAGS = -lpthread

Если в вашем коде нет ошибок (я не проверял), это должно решить вашу проблему ...

0 голосов
/ 05 декабря 2011

как это:

   gcc -o pthread pthread.c  -lpthread
0 голосов
/ 10 ноября 2010

Добавить -pthread в строку gcc cmd.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...