对于我有一个库的东西,未定义的引用

时间:2022-02-14 09:44:18

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

在编译用C语言编写的学校项目时,我得到以下错误:

/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

编辑2.0:相同的错误错误

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.

编辑3.0:成功编译。谢谢Paul Griffiths和Joachim Isaksson。

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

2 个解决方案

#1


3  

For sem_* functions, link with -pthread.

对于sem_ *函数,请链接-pthread。

For shm_* functions, link with -lrt.

对于shm_ *函数,请链接-lrt。

#2


2  

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

可能你需要链接那些sem_ *方法所在的库。

-lpthread

#1


3  

For sem_* functions, link with -pthread.

对于sem_ *函数,请链接-pthread。

For shm_* functions, link with -lrt.

对于shm_ *函数,请链接-lrt。

#2


2  

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

可能你需要链接那些sem_ *方法所在的库。

-lpthread