• 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.
  • Posts

    • The Start menu in Windows 11 is the Start menu I dislike the most.
    • Apple's AI models still trail behind OpenAI's GPT-4o despite latest update by Pradeep Viswanathan At WWDC 2025, Apple announced several updates related to Apple Intelligence for both developers and consumers. With the new Foundation Models framework, developers can now bring AI experiences to their apps that work offline in a privacy-preserving way and are available free of charge. The Foundation Models framework is built on Apple’s own in-house-developed AI models. Apple also unveiled a new generation of language foundation models. According to Apple, these updated models are faster, more efficient, and offer improved tool use, better reasoning capabilities, multimodal support for image and text inputs, and support for 15 languages. Apple Intelligence includes two foundation models: A 3-billion-parameter model that runs on-device using Apple Silicon. A server-based mixture-of-experts model optimized for Private Cloud Compute. Apple noted that the on-device 3B language model is not designed to be a general-purpose chatbot. Instead, it is intended to perform text-related tasks such as summarization, entity extraction, text understanding, refinement, short dialogues, and creative content generation, among others. The big question is how well Apple’s models perform compared to other leading models on the market. Rather than using standard AI benchmarks, Apple shared results from its own internal evaluations of fundamental language and reasoning capabilities. According to Apple’s text-based evaluations, its on-device 3B model performs favorably against the slightly larger Qwen-2.5-3B and competitively against the larger Qwen-3-4B and Gemma-3-4B in English. Its server-based model performs slightly better than Llama-4-Scout but falls short compared to Qwen-3-235B and OpenAI’s proprietary GPT-4o. In evaluations involving image input, Apple’s on-device model outperforms InternVL and Qwen, and performs competitively against Gemma. While Apple’s server model beats Qwen-2.5-VL, it underperforms when compared to Llama-4-Scout and GPT-4o. These results highlight how far Apple still has to go in foundational AI capabilities. It seems Apple compared its models to GPT-4o to make its performance appear relatively decent. If Apple were to compare its results against OpenAI’s latest O-series models or Google’s Gemini 2.5 Pro, the gap would likely appear much wider. It will be interesting to see how Apple navigates the AI era with its in-house capabilities in the years ahead.
    • Voe 3 video generation has gone viral. Especially with audio they look realistic. People posting tons of wild videos created by ai is insane
    • Vive tool again. Sigh, no thanks.
  • Recent Achievements

    • Week One Done
      IAMFLUXX earned a badge
      Week One Done
    • One Month Later
      Æhund earned a badge
      One Month Later
    • One Month Later
      CoolRaoul earned a badge
      One Month Later
    • First Post
      Kurotama earned a badge
      First Post
    • Collaborator
      Carltonbar earned a badge
      Collaborator
  • Popular Contributors

    1. 1
      +primortal
      492
    2. 2
      ATLien_0
      268
    3. 3
      +FloatingFatMan
      231
    4. 4
      +Edouard
      199
    5. 5
      snowy owl
      153
  • Tell a friend

    Love Neowin? Tell a friend!