Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use pthread header file in C project in CLion which uses MinGW in windows

Tags:

c

pthreads

I am trying to include #include <pthread.h> in my project which uses CLion but I am cannot use it directly. Is there any specific way to include pthread to a C project?

like image 333
Demotte Avatar asked Apr 27 '20 10:04

Demotte


People also ask

Does MinGW support Pthread?

The MinGW has already support POSIX threads, In the "MinGW Installation Manager", we can install the pthreads dev package and pthreads lib.

How do I add a header file to clion?

Create a new C/C++ header In the Project tool window, select the directory in which you want to add new files. Right-click it and select New | C/C++ Header File from the context menu. In the dialog that opens: Specify the name of a file.


Video Answer


2 Answers

I finally came up with a solution. Since I am using MinGW I Used MinGW installation manager and install packages that need to execute pthreads and openmp related tasks in CLion. Here is the procedure.

After opening the installation manager go to all packages and select the select packages named using mingw32-pthreads-w32 and select them for installation.

enter image description here

Then go to the installtion -> Apply changes to install new packages. The you can use pthread.h and omp.h inside your c or c++ program without any problem.

like image 159
Demotte Avatar answered Oct 25 '22 21:10

Demotte


Add -lpthread flag to the compilation, i.e:

 gcc foo.c -lpthread

more info here: Why do you need '-lpthread'?

like image 3
MrBens Avatar answered Oct 25 '22 21:10

MrBens