haunted85
haunted85

Reputation: 1671

Compiling issue: undefined reference to pthread_cleanup_push_defer_np() and pthread_cleanup_pop_restore_np()

I am currently writing a C program with threads and I make use of pthread_cleanup_push_defer_np() and pthread_cleanup_pop_restore_np(). Provided that:

when compiling I get back a nasty undefined reference error to the above mentioned functions and I can't figure out why. I have tried to take a look into pthread.h and it seems those two functions are commented out, so I am wondering whether I need to enable them in some way or use some other kind of options. I've read the manual pages and google it up but I can't find a solution, so I would appreciate a little help. Here it is a snippet:

    void *start_up(void *arg)
    {
        char *timestamp;
             // ... code ...
        timestamp = get_current_timestamp("humread"); 
        pthread_cleanup_push_defer_np(free, timestamp);
            // ... some other code
        pthread_cleanup_pop_restore_np(1);
           // ... more code ...
    }

I compile with

gcc -pthread -o server *.c

Upvotes: 0

Views: 221

Answers (1)

P.P
P.P

Reputation: 121397

The manual of pthread_cleanup_push_defer_np and pthread_cleanup_pop_restore_np say these two (non portable) functions are GNU extensions and are enabled by defining _GNU_SOURCE:

   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

       pthread_cleanup_push_defer_np(), pthread_cleanup_pop_defer_np():
           _GNU_SOURCE

This results in linker error (as opposed to compile time error) because your compiler is pre-C99 (or you are compiling in pre-C99 mode) and assumes these functions return int by default.

The rule functions-return-int if no prototype is present has been removed since C99. Enabling more compiler switches can help you with better diagnostics.

Upvotes: 1

Related Questions