"undefined reference to" 或 "implicit declaration of function" pthread_cond_*
"undefined reference to" or "implicit declaration of function" pthread_cond_*
正在尝试 #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
一个临时解决方案:注释掉 -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)
但这并不理想。
没有办法做到这一点,因为 pthread_rwlock* 在 1993 版的 POSIX 规范中不可用。因此,如果您通过添加 -D_POSIX_C_SOURCE=199309L
专门要求 1993 POSIX 规范,那么您将无法使用 1993 规范中不存在的功能。
所以,不要那样做。
是否使用-std=c99
无关紧要;用不用随心所欲
正在尝试 #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
一个临时解决方案:注释掉 -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)
但这并不理想。
没有办法做到这一点,因为 pthread_rwlock* 在 1993 版的 POSIX 规范中不可用。因此,如果您通过添加 -D_POSIX_C_SOURCE=199309L
专门要求 1993 POSIX 规范,那么您将无法使用 1993 规范中不存在的功能。
所以,不要那样做。
是否使用-std=c99
无关紧要;用不用随心所欲