Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add the '-pthread' option to g++ via Eclipse?

Tags:

gcc

eclipse

g++

I am compiling a multithreaded library using eclipse.

How can i add the -pthread or -pthreads option to the call made to g++ by eclipse?

like image 351
Sahil Sachdeva Avatar asked Feb 10 '14 17:02

Sahil Sachdeva


People also ask

What is pthread option?

-pthread Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

Where do you get pthread?

The pthreads run time library usually lives in /lib, while the development library usually lives in /usr/lib.


1 Answers

The original answer that I wrote almost 2 years ago is now outdated; here is how to do it today.

Either right click on the project folder or File > Properties, then C/C++ Build > Settings. At the GCC C++ Linker > Linker flags add -pthread and finally hit the Apply button. That's all.

I have tested this with gcc 4.8 and Eclipse Luna on Ubuntu 14.04. Thank to Neil Traft for pointing it out in a comment.



The original answer from February 2014, now outdated:

Either right click on the project folder or File > Properties, then C/C++ Build > Settings.

At the GCC C++ Compiler > Miscellaneous add -pthread to Other flags and hit the Apply button.

At the GCC C++ Linker > Libraries click the icon with the green plus sign on the Libraries bar to add pthread and finally hit the Apply button.

This must solve it.

Note that you have to tell both the compiler and the linker that you are compiling a multi-threaded application (passing -pthread to the compiler and -lpthread to the linker).

like image 80
Ali Avatar answered Sep 21 '22 11:09

Ali