Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h> 
#include <unistd.h>
#include <pthread.h>

void* thread_main(void *argument){

        int i;
        int cnt = *((int*)argument);

        for(i = 0; i < cnt; i++){
                sleep(1);
                puts("working....");
        }
        return NULL;
}


int main(){

        pthread_t t_id;
        int thread_param = 5;

        if(pthread_creat(&t_id, NULL, thread_main, (void*)&thread_param) != 0){
                puts("pthread_creat() error ");
                return -1;
        };

        sleep(10);
        puts("main end");
        return 0;
}



>> gcc -o thread thread.c -lpthread
thread.c: In function ‘main’:
thread.c:23:5: warning: implicit declaration of function ‘pthread_creat’ [-Wimplicit-function-declaration]
  if(pthread_creat(&t_id, NULL, thread_main, (void*)&thread_param) != 0){
     ^
/tmp/ccERlxNy.o: In function `main':
thread.c:(.text+0x81): undefined reference to `pthread_creat'
collect2: error: ld returned 1 exit status



It's test for basic working thread. but I can't compile this. what's the problem?

OS is linux ubuntu 64bit

What I have tried:

i try to link use gcc -l option, but it was not working.

thread.c:(.text+0x81): undefined reference to `pthread_creat' in google search, every answer are same, just add -lpthread . what the problem

header files




C++
#include <stdio.h> 
#include <unistd.h>
#include <pthread.h></pthread.h></unistd.h></stdio.h>
Posted
Updated 2-Apr-16 6:20am
v4
Comments
Member 12417947 26-Mar-16 11:37am    
headers are


stdio, unistd, pthread
Sergey Alexandrovich Kryukov 26-Mar-16 15:23pm    
Why are you saying that, instead of editing your question? You just have to escape HTML < >, not the rocket surgery... :-)
—SA

Try spelling pthread_create correctly, as shown at pthread_create(3) - Linux manual page[^].
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Mar-16 15:24pm    
:-) :-), a 5.
—SA
pthread_create
You missed e in the end.
Better to use some IDE like eclipse for your dev work, where you can simply avoid these kind of mistakes and save time.
Eclipse is free.
You can also use code block or code light.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900