Chris Happy
Chris Happy

Reputation: 7295

"undefined reference to" or "implicit declaration of function" pthread_cond_*

Trying to #include <pthread.h> for pthread_rwlock_* functions.

But the code errors (see below) unless I comment -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)

Is there any way to compile code while specifying -std and -D_POSIX_C_SOURCE? If so, how could I find the information next time?


Errors: 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'

Errors: 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

Upvotes: 0

Views: 746

Answers (2)

MadScientist
MadScientist

Reputation: 101041

There's no way to do that, because pthread_rwlock* was not available in the POSIX spec in the 1993 version. So if you specifically ask for the 1993 POSIX spec by adding -D_POSIX_C_SOURCE=199309L then you can't use features that weren't present in the 1993 spec.

So, don't do that.

Whether you use -std=c99 is irrelevant; you can use it or not as you like.

Upvotes: 3

Chris Happy
Chris Happy

Reputation: 7295

One temporary solution: just comment out -std=c99 -D_POSIX_C_SOURCE=199309L

Full code below:

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)

But this is not ideal.

Upvotes: 0

Related Questions