0

I want to compile a very program for my Raspberry Pi that uses the thrd_sleep() function as defined in header file in the c11 standard library. I want to use the C11 library for cross-platform compatibility.

A google search I previously did led me to believe that updating gcc should update the available headers because it did not mention any other commands. I did not change the package source for various reasons. I have installed the newest available versions of gcc: gcc-4,6, gcc-4.7, and gcc-4.8. I have tried all of the commands:

gcc main.c

gcc-4.6 main.c

gcc-4.7 main.c

gcc-4.8 main.c

and I get the same error every time:

main.c:2:21: fatal error: threads.h: No such file or directory

It is now evident that I need to install something else. My other searches have failed to conclude anything.

  1. What package includes the C11 headers

  2. Do I have to link any libraries to include the C11 thread support features?

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
user2350838
  • 139
  • 6

3 Answers3

0

For a C++ project I did on the Raspberry Pi, I was using pthreads. Here's my make command:

g++ -o video7 -lpthread -lrt video7.cpp stackstring.cpp playvideo.cpp dirlist.cpp cpuinfo.cpp faststring.cpp liststring.cpp remoteupdate.cpp kbhit.cpp randomnumber.cpp blowfish.cpp base85.cpp

I was including pthread with

#include <pthread.h>

I created a thread like this.

pthread_t updateThread;
char *param = strdup("Sample");
pthread_create(&updateThread,NULL, UpdateThreadProc, param);

void *UpdateThreadProc(void *parameters)
{
   // Do work.
   pthread_exit(NULL);
}
Russell Hankins
  • 1,196
  • 9
  • 17
  • For cross-platform compatibility, I want to use standard C whenever possible. That way I can compile the code with minimal changes on windows and linux without needing cygwin on the windows machine. I appreciate your answer though, and I will learn and use pthread as you suggest, until I can get the C headers updated on linux. – user2350838 Feb 13 '16 at 06:14
  • For the Raspberry Pi, I recently switched to using C# with Mono and had good results. I use System.Windows.Forms for the gui. Or for a terminal program, I used Console.Write. The executable can be moved to a Windows PC without recompiling it. – Russell Hankins Feb 13 '16 at 13:42
0

The thread functions are usually not part of the compiler distribution but are part of the C library, so you'd have to find one that implements the C11 thread interface. Off the open source libraries I only know of musl that implements this interface. There are some Linux distributions that use this C library as their main library, e.g Alpine. On other distributions you may find a package that is ready to be applied, or you may compile musl yourself.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
0

C11 threads are not yet implemented by GCC, see this. Unfortunately, C11 threads are still "new" so they are barely supported by any compilers at this point. Using them for cross-platform development is not a good idea (yet).

POSIX threads is currently the only portable thread library out there.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • AFAIK not GCC but glibc needs to implement C11 threads. C11 threads only needs library support as other features like Thread Local Storage or Atomic Operations need compiler support. See Keith Thompsons answere [here](http://stackoverflow.com/a/8860111/5444148) – fsasm Feb 26 '16 at 20:12