• 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

    • This was cool back in the day when done properly - loved having icons of specific devices.
    • Microsoft quietly burying a massive Windows 7 hardware driver feature as Windows 11 kills it by Sayan Sen Last month Microsoft announced a big update for Windows hardware drivers. The company declared that it was killing Windows Device metadata and the Windows Metadata and Internet Services (WMIS). For those wondering what it is, device metadata, as the name suggests, is the collection of additional, user-facing information that an original equipment manufacturer (OEM) provides about a hardware device. The feature was introduced with Windows 7 and can include stuff like icons, logos, descriptive texts, among other things, that help the Windows UI display details about such devices in places like Task Manager or Device Manager. This was a huge deal back in the day when Windows 7 debuted. The company called the feature "Device Stage" and Microsoft described it as a "new visual interface" that essentially worked like a "multi-function version of Autoplay where it displays all the applications, services, and information related to your device." It is often considered synonymous with the Windows "Devices and Printers" Control Panel applet. Neowin did an in-depth overview of the feature when it first launched which you can find in its dedicated article here. The Windows OS was able to obtain the device experience metadata from the WMIS, but now that the feature is being deprecated, Microsoft has begun removing information about Device Stage from its official support documents. Neowin noticed while browsing that a support article regarding automatic Windows hardware drivers was updated for Windows 11 and 10 sometime last year after the release of Windows 11 24H2. Previously, this article was geared for Windows 7 and was much longer. It also contained information about Device Stage, which, as mentioned above, was a headlining feature on Windows 7. In the said article, the section "If Windows can't find information about your device in Device Stage" has been deleted. You can find the archived version of the support page here. Aside from shortening the amount of information on the page, Microsoft has also added some more details on it. The company has now tried to define what the Microsoft Basic Display Adapter is, how updating drivers through Device Manager works, as well as a thorough and detailed troubleshooting section for common hardware driver errors on Windows, including one for USB-C. You can find all the new details on the updated support page here on Microsoft's website.
    • Sounds creepy to say the least. Don't need nor want AI having access to my history. They're claiming it to be an "offline" model now, but how can we guarantee they don't go behind our backs and change that?
    • Exactly! Without those fundamentals you've mentioned, Democracy is literally just Demonstration of Crazy, nothing to be proud of in such system.
  • Recent Achievements

    • Veteran
      Yonah went up a rank
      Veteran
    • First Post
      viraltui earned a badge
      First Post
    • Reacting Well
      viraltui earned a badge
      Reacting Well
    • Week One Done
      LunaFerret earned a badge
      Week One Done
    • Week One Done
      Ricky Chan earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      481
    2. 2
      +FloatingFatMan
      264
    3. 3
      snowy owl
      238
    4. 4
      ATLien_0
      233
    5. 5
      Edouard
      176
  • Tell a friend

    Love Neowin? Tell a friend!