Adding -pthread to eclipse, for using posix threads

15/02/2009

Problem

Cannot compile c/c++ project with -pthread. Eclipse is complaining about things.

Solution

Eclipse is not configured to put the -pthread argument in the gcc compilation. To solve this, go to the Menu:

Project -> Properties

From the bar on the left:

c/c++ build -> GCC C Compiler -> Miscellaneous

Add the “-pthread” argument into the beginning of the “Other Flags” Also go to:

c/c++ build -> Settings -> GCC C Linker -> Libraries

And include the “pthread”library into the other libraries. Click Apply and rebuild the project. Pthreads must work now.

There are 23 comments in this article:

  1. 23/02/2009ahmet say:

    thanks

  2. 10/04/2009rajend say:

    this one helped me too…
    thanks a lot

  3. 4/07/2009fork() a child say:

    it helped me too… thanks!

  4. 28/10/2009alex say:

    +1

  5. 21/11/2009nuno say:

    thanks a lot:D

  6. 6/01/2010vinod say:

    Thanks dude, yu rock…

    V…

  7. 23/02/2010aaron say:

    Thank you.

  8. 5/03/2010alex say:

    thanks a lot..

  9. 15/04/2010dahdash say:

    tnx ;)

  10. 17/10/2010donat3llo say:

    thanks a bunch bro, works like a charm for me.

  11. 21/11/2010david say:

    thank you very mutch god bless you

  12. 23/11/2010Gluko say:

    Thanks!!! very helpful

  13. 23/02/2011Fred say:

    Thanks!

  14. 29/04/2011Dolcezeus say:

    don’t work for me.. i have an linker error the option is present on compiler but not on Linker console outpt.. please help me, if i compile manually the program work fine, but in eclipse i can’t buid it.

  15. 1/05/2011desufag say:

    Thank you, really helped.

  16. 9/06/2011Abraham say:

    Does anyone know how to debug pthreads in eclipse? I tried using breakpoints but I never hit them; however the console output tells me the code is indeed running and outputting correctly.

  17. 14/06/2011Roy say:

    Thank you ..really helped

  18. 18/09/2011cash say:

    thank a lot :)

  19. 2/10/2011some dude say:

    awesome simple fix. thanks

  20. 26/10/2011Cem say:

    thanks my brother, it has worked but i didnt understand why we had to do this configuration

  21. 6/11/2011Hashim Ali say:

    Hello!
    Thanks for the post.
    I have one question, What do you think if I have requirement of non-blocking thread kind of functionality but not using threads?
    Many thanks for help.

  22. 23/11/2011Donald say:

    don’t work for me.Build the project, the console give me such messages:

    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1212: error: expected constructor, destructor, or type conversion before “pthread_getw32threadhandle_np”
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1230: error: variable `pthreadCancelableWait’ definition is marked dllimport.
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1230: warning: `__cdecl__’ attribute only applies to function types
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1230: error: `HANDLE’ was not declared in this scope
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1231: error: variable `pthreadCancelableTimedWait’ definition is marked dllimport.
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1231: warning: `__cdecl__’ attribute only applies to function types
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1231: error: `HANDLE’ was not declared in this scope
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1232: error: `DWORD’ was not declared in this scope
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1232: error: initializer expression list treated as compound expression
    D:/NEAT/include/neat/ctrl/../win32/pthread.h:1311: error: expected constructor, destructor, or type conversion before “ptw32_get_exception_services_code”

    Could anyone help me ?
    thanks in advance!

  23. 18/01/2012kyle say:

    thanks a ton. this helped

Write a comment: