• 0

Compiling pthread program


Question

Hello, I am trying to compile a simple C program that uses pthreads. It's one of the tutorial examples at https://computing.llnl.gov/tutorials/pthreads , more precisley this : https://computing.llnl.gov/tutorials/pthrea...mples/condvar.c

First I tried it with Code::Blocks in my Ubuntu VM, but Code::Blocks says pthread is an undefined reference and when I try to open the include file (by right clicking <pthread.h> and clicking "open file" or something like that), it says the file can't be found. So I guess Code::Blocks doesn't know where it is. Furthermore, I tried the same with stdlib.h and others and it can't open any of them. How can I configure it so that it compiles properly ? I just created an empty C++ project and copy-pasted the code in main.cpp.

Next I tried in Visual Studio 2008 under Windows. Now, VS can open all the other include files alright, but not pthread.h, and upon compilation it will complain that it cannot open include file 'pthread.h'. A quick search on Google gives very little insight.

I just want to compile and run the damn thing, any environment/editor will do ! Thanks for your help.

Link to comment
https://www.neowin.net/forum/topic/749278-compiling-pthread-program/
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Just to add to my previous post :

Under Code::Blocks, although I cannot open any include file, programs that use standard C and C++ librairies run fine. Such as iostream and stdlib Hello Worlds.

The errors I get are all "undefined reference to 'pthread_create'.

I have tried adding -lpthread to the compiler options, but it didn't fix the problem.

  • 0

So in my stubberness, I managed to make the program compile under Code::Blocks. It involved adding the "pthread" library in the linker settings.

I still wonder if it's possible to do it under Visual Studio.

EDIT: just tried it with Code::Blocks under Windows this time. Adding the pthread library does absolutely nothing this time. Also, Code::Blocks seems to work more sensibly under Windows, for instance it opens include files correctly. But it won't open pthread.h, just like Visual Studio.

Edited by Dr_Asik
  • 0

Yeah you need to include pthread in the linker stage. I've used pthreads before but only under Linux and it's a bit different to how I included the library the last time I used it.

I use Dev C++ as my IDE and I first created a C project. I think it's still possible to do it under Visual

Studio though but I don't have any experience with it. Before getting it to work in Dev I downloaded the pthreads library from here. Looking at the read me

  Quote
Which of the several dll versions to use?

-----------------------------------------

or,

---

What are all these pthread*.dll and pthread*.lib files?

-------------------------------------------------------

Simple, use either pthreadGCv.* if you use GCC, or pthreadVCv.* if you

use MSVC - where 'v' is the DLL versioning (compatibility) number.

Otherwise, you need to choose carefully and know WHY.

So I then copied the header files (pthread, sched and semaphore) to the dev include library and put the pthreadGC2.dll in my project folder where the .exe will be created.

In Dev I then added -lpthreadGC2 at the linker stage and it compiled OK. Hope this helps.

edit: Never even noticed Mike got a post in before me :laugh:

Edited by ViZioN
  • 0

Visual Studio is a C++ compiler, I don't even think it is fully C89 compliant. But most new C99 features will not work in VS 2008, they are trying to follow C++ standards not C, any C code you should compile with GCC (MinGW32 on windows). Make sure to link any libraries supplied with the code, and a direct reference to the header file "pthread.h" should be made, not <pthread.h>.

  • 0
  chhavi said:
Hello,

I am using CodeBlocks and getting problem in the compilation of a thread program. It says "can not find pthread.h"

I tried adding "pthread" in the linker settings, still fails. Could you please let me know the solution to this problem.

Thanks in advance.

Follow my post above exactly. It was for Dev C++ not Code::Blocks but the process should be almost identical.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.