Ubuntu – Multithreading in codeblocks


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

void *myThreadFun(void *vargp)

    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");

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:


Best Answer

  • 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

  • Related Question