【问题标题】:"undefined reference to" or "implicit declaration of function" pthread_cond_*“未定义的引用”或“函数的隐式声明”pthread_cond_*
【发布时间】: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

【问题讨论】:

标签: c makefile pthreads locking


【解决方案1】:

没有办法做到这一点,因为 pthread_rwlock* 在 1993 版本的 POSIX 规范中不可用。因此,如果您通过添加 -D_POSIX_C_SOURCE=199309L 特别要求 1993 年 POSIX 规范,那么您将无法使用 1993 年规范中不存在的功能。

所以,不要那样做。

是否使用-std=c99 无关紧要;可以用也可以不用。

【讨论】:

  • 此外,我通常建议避免在命令行上设置功能测试宏作为一般规则。特定源文件所需的功能是该文件的特征,因此如果需要定义功能测试宏,那么这就是定义应该去的地方。
【解决方案2】:

一个临时解决方案:只需注释掉 -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)

但这并不理想。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-16
    • 2018-06-11
    • 2013-04-09
    • 2021-01-20
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    相关资源
    最近更新 更多