• 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

    • Apple took Windows Aero Glass to perfection, while Microsoft went to an interface that is a visually jarring mess, stripped of clear visual cues. Everything looks like a random pile of flattened, ambiguous elements thrown together without coherence. It’s hard to tell what anything is supposed to do, let alone where it originally came from.
    • It's very pretty, IMO. I've always loved the Windows 7-style of UI design. 7 was the prettiest Windows ever. I'd love Microsoft to be THIS consistent and I'd love them to return to that.
    • Here are the Macs that support macOS 26 Tahoe by Taras Buria In addition to iOS 26, iPadOS 26, watchOS 26, and tvOS 26, Apple announced macOS 26 Tahoe, a new desktop operating system. Like the other operating systems made in Cupertino, macOS Tahoe features a redesigned UI with Liquid Glass, translucent icons, and other visual upgrades. There are also plenty of new features, such as a new Phone app for Mac, Live Activities on Mac, a massive Spotlight update, and a lot more. However, not all Mac owners will get to enjoy macOS 26. Like with iOS and iPadOS, Apple is dropping a few Macs with the release of macOS 26. You will be able to update to the new operating system if you own one of the following Apple computers. MacBook Air M1 and newer MacBook Pro M1 and newer MacBook Pro 13-inch 2020 (four Thunderbolt 3 ports) and newer MacBook Pro 16-inch 2019 iMac 2020 and newer Mac mini 2020 and newer Mac Studio 2022 and newer Mac Pro 2019 and newer Note that although Apple still supports Intel-based Macs, not all features will be available on systems that do not have Apple Silicon chips. Now-unsupported Macs that cannot upgrade from macOS Sequoia to macOS Tahoe are the following computers with Intel processors: Intel-based iMac 2019 Intel-based Mac mini 2018 iMac Pro Intel-based MacBook Air 2020 Intel-based MacBook Pro 2018 macOS 26 Tahoe is now available for developers to try in the beta program. Apple plans to launch the first public beta in July, alongside the release of public betas of iOS 26, iPadOS 26, and other operating systems that the company announced earlier today. You can find all the new features in macOS 26 Tahoe on the official Apple website.
    • I hope Microsoft goes back to the Fisher Price XP look first.
  • Recent Achievements

    • Rookie
      CHUNWEI went up a rank
      Rookie
    • Enthusiast
      the420kid went up a rank
      Enthusiast
    • Conversation Starter
      NeoToad777 earned a badge
      Conversation Starter
    • Week One Done
      VicByrd earned a badge
      Week One Done
    • Reacting Well
      NeoToad777 earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      478
    2. 2
      +FloatingFatMan
      272
    3. 3
      ATLien_0
      256
    4. 4
      Edouard
      203
    5. 5
      snowy owl
      191
  • Tell a friend

    Love Neowin? Tell a friend!