簡體   English   中英

對於我擁有的庫,未定義引用

[英]undefined reference to something for what I have a library for

編譯我用CI編寫的學校項目時,出現以下錯誤:

/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)

我的圖書館是:

#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>

我正在使用以下代碼進行編譯:

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

有任何想法嗎?

(我也很ham愧地承認我將所有內容都放在一個主干中。是的。請不要對我投以石頭。)

編輯:因此,我將編譯設置調整為:

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

投訴數量減少到只有以下兩種:

/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:相同的錯誤錯誤

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

編輯3.0:成功編譯。 謝謝Paul Griffiths和Joachim Isaksson。

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

對於sem_*函數,請-pthread鏈接

對於shm_*函數,請-lrt鏈接

可能需要鏈接到那些sem_*方法所在的庫。

-lpthread

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM