please help in compiling this program
#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;
// crea开发者_运维知识库te 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
You need to add the -pthread flag to your compiler.
You've not included the pthread library. Try adding
-lpthread
to your compliation line.
You're not linking with libpthread. And manually compiling with gcc is really BFI. Put your code in a file called 'thread_test.c', and then do:
make thread_test LDFLAGS=-lpthread
Assuming no errors in your code (I didn't check), this should fix your problem...
Add -pthread to gcc cmd line.
like this:
gcc -o pthread pthread.c -lpthread
精彩评论