2020-11-24 13:27

Mac Os Warning: implicit declaration of function 'pthread_setname_np'

Compiling thpool on Mac Os with lldb 370.0.42 triggers the above message.

cc -Wall -D_GNU_SOURCE -std=gnu99 -O2 -g  -c -o dep/thpool/thpool.o dep/thpool/thpool.c
dep/thpool/thpool.c:298:3: warning: implicit declaration of function 'pthread_setname_np' is invalid in C99


#define _POSIX_C_SOURCE 200809L

which was added in a3916021c837e4892667e02e1681437ddc362725 gets rid of the warning, and passes on Linux as well.

Any hint on how to fix this warning? Is it safe to just comment out the _POSIX_C_SOURCE line?


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • weixin_39800918 Hu?? 5月前

    I can't really recall exactly the reasoning behind it (my commit should probably be more informative).

    However my gut feeling is that the macro is overriding existing information in your case. Do you think you can try the below on a Mac and see if it solves it?

    #if ! defined(_POSIX_C_SOURCE)
    #define _POSIX_C_SOURCE 200809L
    点赞 评论 复制链接分享
  • weixin_39822993 weixin_39822993 5月前

    nope, same results.


    点赞 评论 复制链接分享
  • weixin_39562998 weixin_39562998 5月前

    the way these macros normally work is that they make your compiler run in a specific 'mode'. According to the POSIX standard (http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_02.html):

    Additional symbols not required or explicitly permitted by IEEE Std 1003.1-2001 to be in that header shall not be made visible, except when enabled by another feature test macro.

    It might be the case that you need to include an os-x specific header (maybe *mach*.h) to get the _np functionality.

    点赞 评论 复制链接分享
  • weixin_39562998 weixin_39562998 5月前

    FWIW, this is what's on my system. All the _np decls are guarded by this. It seems you might have luck with _DARWIN_C_SOURCE.

    #if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE)
    /* returns non-zero if pthread_create or cthread_fork have been called */
    int pthread_is_threaded_np(void);
    点赞 评论 复制链接分享
  • weixin_39616367 weixin_39616367 5月前

    On top use that instead of forcing the _POSIX_C_SOURCE define :

    #   if defined(__APPLE__)
    #       include <availabilitymacros.h>
    #   else
    #       ifndef _POSIX_C_SOURCE
    #           define _POSIX_C_SOURCE 200809L
    #       endif
    #   endif

    c99-pedantic.patch (Tested : compile without any warnings or errors (BTW you can't dereference func_ptr to void* : nada verboten): on Debian 7, iOS (min-version 6.0) and MacOS (min-version 10.7), Android (min-version lollipop) gcc, gcc-clang and clang toolchains)

    --- thpool_original.c   2018-03-21 13:46:04.000000000 -0700
    +++ thpool_pedantic.c   2018-03-21 13:45:05.000000000 -0700
    @@ -8,7 +8,17 @@
    -#define _POSIX_C_SOURCE 200809L
    +#  if defined(__APPLE__)
    +#      include <availabilitymacros.h>
    +#  else
    +#      ifndef _POSIX_C_SOURCE
    +#          define _POSIX_C_SOURCE 200809L
    +#      elif _POSIX_C_SOURCE < 200809L
    +#          error "Valid _POSIX_C_SOURCE version required."
    +#      endif
    +#  endif
     #include <unistd.h>
     #include <signal.h>
     #include <stdio.h>
    @@ -94,7 +104,7 @@
     static int  thread_init(thpool_* thpool_p, struct thread** thread_p, int id);
    -static void* thread_do(struct thread* thread_p);
    +static void* thread_do(void * arg);
     static void  thread_hold(int sig_id);
     static void  thread_destroy(struct thread* thread_p);
    @@ -123,8 +133,8 @@
        threads_on_hold   = 0;
        threads_keepalive = 1;
    -   if (num_threads < 0){
    -       num_threads = 0;
    +   if (num_threads < 1){
    +       num_threads = 1;
        /* Make new thread pool */
    @@ -290,7 +300,7 @@
        (*thread_p)->thpool_p = thpool_p;
        (*thread_p)->id       = id;
    -   pthread_create(&(*thread_p)->pthread, NULL, (void *)thread_do, (*thread_p));
    +   pthread_create(&(*thread_p)->pthread, NULL, thread_do, (*thread_p));
        return 0;
    @@ -314,8 +324,13 @@
     *   thread        thread that will run this function
     *  nothing
    -static void* thread_do(struct thread* thread_p){
    +static void* thread_do(void * arg){
    +   struct thread * thread_p = (struct thread *)arg;
    +   if (thread_p == NULL){
    +       err("thread_do(): Could not access cookie\n");
    +       return NULL;
    +   }
        /* Set thread name for profiling and debuging */
        char thread_name[128] = {0};
        sprintf(thread_name, "thread-pool-%d", thread_p->id);
    点赞 评论 复制链接分享
  • weixin_40008566 weixin_40008566 5月前

    Hi ! I'm having the same warning

    点赞 评论 复制链接分享