How to enable Pthread support in Eclipse CDT IDE?

Eclipse is a wonderful open source integrated development environment. It was primarily created to support Java development but was later extended to support other programming languages.

To support programming in other languages, plugins were created as an add-on to the core Eclipse IDE. One such plugin emerged out of the Eclipse CDT project. The term ‘CDT’ stands for ‘C/C++ Development Tooling’. As the name implies, it provides a full functional integrated development environment based on the core Eclipse platform to assist in the development of projects in C and C++ programming language.’

This post describes how to update the configuration of the Eclipse CDT IDE to support successful compilation of C/C++ projects that utilize the ‘Pthread’ library.

Pthread is an acronym for ‘POSIX Threads’. An earlier post titled – Program v/s Process v/s Thread, sheds light on the concept of threads, process, program and their relationship.

If your are using Eclipse CDT IDE for developing your C/C++ project and if your projects utilizes the ‘Pthread’ library, then there exists a fair probability that you would have encountered the compilation error indicating that the required ‘Pthread’ library was not included as part of compilation process.

The core issue here is the CDT Plugin. By default, the plugin is not configured to append the argument ‘-pthread’ while compiling C/C++ projects that rely on the Pthread library.

The below outlined steps would allow you to successfully compile your projects that depend on ‘Pthread’ library:

Step 1: Set the Compiler options.

  • Open the Eclipse IDE (it is assumed that the CDT plugin has already been installed).
  • On the ‘Project Explorer’ pane, right click on the name of the project which utilizes the ‘Pthread’ library and choose ‘Properties’.
  • In the left side bar of the ‘Properties’ dialog box, please choose ‘C/C++ build –> GCC C Compiler –> Miscellaneous’.
  • Insert the term ‘-pthread’ against the option ‘Other Flags’.

Step 2: Update the Linker options.

  • In the left side bar of the ‘Properties’ dialog box, please choose ‘C/C++ build –> Settings –> GCC C Linker –> Libraries’.
  • Include the ‘-pthread’ library in the other libraries section.
  • Click ‘Apply’ and rebuild the project. Pthread should work as expected now.



5 thoughts on “How to enable Pthread support in Eclipse CDT IDE?

  1. Thanks for this!!! I’ve been puling my hair out the past two weeks looking for this info!! (Learning C++ and new to Eclipse).

    Most tut’s are for different versions, so the options haveall changed and my ignorancedoesn’t help either.

    Tried your solution. BOOM!!! Now multithreading (took all of 30 seconds to set up).

    Thanks so much for posting this!!!

  2. Step 2 is incorrect.

    When linking to the pthread lib, you only need to write “pthread”.
    It automatically turns into “-lpthread” which is used to compile.

  3. j’ai essayé ta methode mais ca ne marche pas chez moi, en fait je suis sur eclipse CDT pour la version linux.lorsque je compile ca me donne les erreurs suivantes:
    -undefined reference to `main’
    -cannot find -l-pthread.
    merci de me repondre.

    1. Thank you for taking time to write your comment. A quick guess would be to check if ‘libpthread’ library is installed in your machine.

Comments are closed.