Question

When compiling my school project written in C I get following errors:

/tmp/ccFDQk9j.o: In function `main':
proj2.c:(.text+0x187): undefined reference to `sem_open' (several times)
proj2.c:(.text+0x35a): undefined reference to `sem_post' (several times)
proj2.c:(.text+0x381): undefined reference to `sem_wait' (several times)
proj2.c:(.text+0x6ec): undefined reference to `sem_close' (several times)
proj2.c:(.text+0xda6): undefined reference to `sem_unlink' (several times)

My libraries are:

#include <errno.h>
#include <fcntl.h>           
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <sys/sem.h>
#include <sys/stat.h>       
#include <sys/types.h>
#include <sys/mman.h>
#include <time.h>

And I am compiling using:

gcc -std=gnu99 -Wall -Wextra -Werror -pedantic proj2.c -o ext

Any ideas?

(Also I am ashamed to admit I put everything into one main. Yeah. Please don't stone me.)

EDIT: So I adjusted my compilation settings to:

gcc -std=gnu99 -Wall -Wextra -Werror -pedantic -pthread proj2.c -o ext

And the amount of complaints has been reduced to these only two:

/tmp/cc51XZFK.o: In function `main':
proj2.c:(.text+0x249): undefined reference to `shm_open'
proj2.c:(.text+0xdf9): undefined reference to `shm_unlink'
collect2: ld returned 1 exit status

EDIT 2.0: Same error mistake for

gcc -std=gnu99 -lrt -Wall -Wextra -Werror -pedantic -pthread proj2.c -o ext

EDIT 3.0: Successfully compiled. Thank you Paul Griffiths and Joachim Isaksson.

gcc -std=gnu99 -Wall -Wextra -Werror -pedantic proj2.c -pthread  -lrt -o ext
Was it helpful?

Solution

For sem_* functions, link with -pthread.

For shm_* functions, link with -lrt.

OTHER TIPS

Probably you need to link with a library where those sem_* methods are.

-lpthread
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top