【发布时间】:2021-06-22 19:44:58
【问题描述】:
尝试使用#include <pthread.h> 获取pthread_rwlock_* 函数。
但代码错误(见下文)除非我评论-std=c99 -D_POSIX_C_SOURCE=199309L。
CCFLAGS = -Wall -g -std=c99 -D_POSIX_C_SOURCE=199309L
LDFLAGS = -pthread
%.o : %.c
$(CC) -c $< $(CCFLAGS) $(LDFLAGS)
$(TARGET): $(OBJS)
$(CC) $(OBJS) -o $@ $(CCFLAGS) $(LDFLAGS)
在指定-std 和-D_POSIX_C_SOURCE 时有什么方法可以编译代码?如果是这样,下次我怎么才能找到信息?
错误: undefined reference to pthread_rwlock_*
myalloc.c:(.text+0x1c): undefined reference to `pthread_rwlock_init'
myalloc.c:(.text+0x47): undefined reference to `pthread_rwlock_init'
myalloc.c:(.text+0x93): undefined reference to `pthread_rwlock_trywrlock'
myalloc.c:(.text+0x195): undefined reference to `pthread_rwlock_unlock'
myalloc.o: In function `destroy_allocator':
myalloc.c:(.text+0x1a8): undefined reference to `pthread_rwlock_trywrlock'
myalloc.c:(.text+0x216): undefined reference to `pthread_rwlock_unlock'
myalloc.c:(.text+0x222): undefined reference to `pthread_rwlock_destroy'
myalloc.o: In function `allocate':
myalloc.c:(.text+0x262): undefined reference to `pthread_rwlock_rdlock'
myalloc.c:(.text+0x27e): undefined reference to `pthread_rwlock_unlock'
myalloc.c:(.text+0x2c0): undefined reference to `pthread_rwlock_trywrlock'
myalloc.c:(.text+0x2e3): undefined reference to `pthread_rwlock_unlock'
myalloc.o: In function `deallocate':
myalloc.c:(.text+0x335): undefined reference to `pthread_rwlock_rdlock'
myalloc.c:(.text+0x35b): undefined reference to `pthread_rwlock_unlock'
myalloc.c:(.text+0x39b): undefined reference to `pthread_rwlock_trywrlock'
myalloc.c:(.text+0x3cd): undefined reference to `pthread_rwlock_unlock'
myalloc.o: In function `compact_allocation':
myalloc.c:(.text+0x40c): undefined reference to `pthread_rwlock_trywrlock'
myalloc.c:(.text+0x5e2): undefined reference to `pthread_rwlock_unlock'
myalloc.o: In function `available_memory':
myalloc.c:(.text+0x5fb): undefined reference to `pthread_rwlock_rdlock'
myalloc.c:(.text+0x649): undefined reference to `pthread_rwlock_unlock'
myalloc.o: In function `print_statistics':
myalloc.c:(.text+0x662): undefined reference to `pthread_rwlock_rdlock'
myalloc.c:(.text+0x759): undefined reference to `pthread_rwlock_unlock'
错误: implicit declaration of function ‘pthread_rwlock_\*’; did you mean...
myalloc.c:56:1: error: unknown type name ‘pthread_rwlock_t’; did you mean ‘pthread_cond_t’?
pthread_rwlock_t freeLock;
^~~~~~~~~~~~~~~~
pthread_cond_t
myalloc.c:57:1: error: unknown type name ‘pthread_rwlock_t’; did you mean ‘pthread_cond_t’?
pthread_rwlock_t allocLock;
^~~~~~~~~~~~~~~~
pthread_cond_t
myalloc.c: In function ‘initialize_allocator’:
myalloc.c:63:9: warning: implicit declaration of function ‘pthread_rwlock_init’; did you mean ‘pthread_cond_init’? [-Wimplicit-function-declaration]
if (pthread_rwlock_init(&freeLock, NULL) != 0)
^~~~~~~~~~~~~~~~~~~
pthread_cond_init
myalloc.c:77:5: warning: implicit declaration of function ‘pthread_rwlock_trywrlock’; did you mean ‘pthread_mutex_trylock’? [-Wimplicit-function-declaration]
pthread_rwlock_trywrlock(&allocLock);
^~~~~~~~~~~~~~~~~~~~~~~~
pthread_mutex_trylock
myalloc.c:112:5: warning: implicit declaration of function ‘pthread_rwlock_unlock’; did you mean ‘pthread_mutex_unlock’? [-Wimplicit-function-declaration]
pthread_rwlock_unlock(&allocLock);
^~~~~~~~~~~~~~~~~~~~~
pthread_mutex_unlock
myalloc.c: In function ‘destroy_allocator’:
myalloc.c:139:5: warning: implicit declaration of function ‘pthread_rwlock_destroy’; did you mean ‘pthread_cond_destroy’? [-Wimplicit-function-declaration]
pthread_rwlock_destroy(&allocLock);
^~~~~~~~~~~~~~~~~~~~~~
pthread_cond_destroy
myalloc.c: In function ‘allocate’:
myalloc.c:154:5: warning: implicit declaration of function ‘pthread_rwlock_rdlock’; did you mean ‘pthread_mutex_unlock’? [-Wimplicit-function-declaration]
pthread_rwlock_rdlock(&allocLock);
^~~~~~~~~~~~~~~~~~~~~
pthread_mutex_unlock
【问题讨论】:
-
我注意到 Google 搜索错误
undefined reference to 'pthread_rwlock_init'显示的操作系统问题多于编译器问题。所以想在这里分享知识。
标签: c makefile pthreads locking