2

I tried to implement multithreading using codeblocks in ubuntu.
I typed the following program.

void *myThreadFun(void *vargp)
{

    sleep(1);
    printf("Printing hi from Thread \n");
    return NULL;
}

int main()
{

    pthread_t tid;
    printf("Before Thread\n");
    pthread_create(&tid, NULL, myThreadFun, NULL);
    pthread_join(tid, NULL);
    printf("After Thread\n");
    exit(0);
}

But I couldn't compile and run the program in codeblocks. I got the following error

multithread.c  undefined reference to 'pthread_create'
multithread.c  undefined reference to 'pthread_join'

can someone help me out

I had used the following header files in the code:

pthread.h
stdio.h
stdlib.h

3 Answers 3

4

In Code::Blocks if you are using the GNU gcc compiler, you will probably need to add the -pthread compiler flag to both the compiler and linker options (under `Project --> Build options...)

Code::Blocks compiler options

Code::Blocks linker options

1
  • Works fine in Code Blocks 13.12, Ubuntu 15.04 64 bit.
    – aghost
    Jun 24, 2015 at 20:02
2

Don't forget to make sure that you installed the pthread librarys correctly. You can find it in the software center under "libpthread-stubs0-dev" or simply install by doing :

sudo apt-get install libpthread-stubs0-dev.

Also I didn't really understand at firt that you just have to type in -pthread manually...

0

Please go through this link, the problem is not with codeblocks and not with the compiler either. Problem seems to be with the linker, i tried your code in eclipse, i get the same error if i am not using:

gcc -pthread -o multithread multithread.c

Hope that helps. Read more here.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .