Why is this semaphore code failing?
#include <semaphore.h>
sem_t mutex;
int main (int argc, char * argv[])
{
sem_init (&mutex,0,1);
}
I'm getting:
/tmp/ccAMFxDX.o: In function开发者_开发百科 `main':
programaservidor.c:(.text+0x86): undefined reference to `sem_init'
collect2: ld returned 1 exit status
Per the sem_init() man page
Link with -lrt or -pthread.
As in gcc your_code.c -lpthread -o your_code
As stated in the man page, you have to link with either -lrt
or -pthread
.
The relevant POSIX reference is:
http://www.opengroup.org/onlinepubs/9699919799/utilities/c99.html#tag_20_11_13
精彩评论