2010-11-10 2 views
0
#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 
+0

Есть вопрос где-то здесь? – Zoot

+0

Я тоже не вижу вопроса, вам нужно уточнить, что вы хотите, или это будет удалено. – prolink007

+0

И вопрос в том, что? –

ответ

8

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

+0

спасибо .. как добавить -pthread в мою программу .. – kot

+0

Добавьте его в свой 'makefile' или 'gcc'. –

+0

Используете ли вы IDE, например eclipse, для создания вашей программы? –

5

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

-lpthread 

к вашей линии слияния.

1

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

сделать thread_test LDFLAGS = -lpthread

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

Смежные вопросы