Skip to content

Commit

Permalink
fix: fix a compilation error on GCC/Linux
Browse files Browse the repository at this point in the history
    xstrdup.c:13:18: warning: implicit declaration of function ‘strdup’; did you mean ‘xstrdup’? [-Wimplicit-function-declaration]
       13 |     char *copy = strdup(str);
          |                  ^~~~~~
          |                  xstrdup

As Stack Overflow says:

> It allows you to use functions that are not part of the standard C
> library but are part of the POSIX.1 (IEEE Standard 1003.1) standard.
> Using the macros described in feature_test_macros allows you to
> control the definitions exposed by the system header files.
>
> As far as I know _POSIX_SOURCE is obsolete and you should use
> _POSIX_C_SOURCE instead.
>
> For example, if you want to use strndup, you have to use
>
> #define _POSIX_C_SOURCE 200809L

https://stackoverflow.com/a/48332467/2103996

And the libc manual:

> Macro: _POSIX_C_SOURCE
>
> Define this macro to a positive integer to control which POSIX
> functionality is made available. The greater the value of this macro,
> the more functionality is made available.
>
> If you define this macro to a value greater than or equal to 1, then
> the functionality from the 1990 edition of the POSIX.1 standard (IEEE
> Standard 1003.1-1990) is made available.
>
> If you define this macro to a value greater than or equal to 2, then
> the functionality from the 1992 edition of the POSIX.2 standard (IEEE
> Standard 1003.2-1992) is made available.
>
> If you define this macro to a value greater than or equal to 199309L,
> then the functionality from the 1993 edition of the POSIX.1b standard
> (IEEE Standard 1003.1b-1993) is made available.
>
> If you define this macro to a value greater than or equal to 199506L,
> then the functionality from the 1995 edition of the POSIX.1c standard
> (IEEE Standard 1003.1c-1995) is made available.
>
> If you define this macro to a value greater than or equal to 200112L,
> then the functionality from the 2001 edition of the POSIX standard
> (IEEE Standard 1003.1-2001) is made available.
>
> If you define this macro to a value greater than or equal to 200809L,
> then the functionality from the 2008 edition of the POSIX standard
> (IEEE Standard 1003.1-2008) is made available.

https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
  • Loading branch information
wincent committed Aug 17, 2024
1 parent 5f8a37f commit 3ff3e4e
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lua/wincent/commandt/lib/xstrdup.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "xstrdup.h"

#define _POSIX_C_SOURCE 200809L /* needed in order to see strdup() */

#include <stddef.h> /* for NULL */
#include <stdlib.h> /* for abort() */
#include <string.h> /* for strdup() */
Expand Down

0 comments on commit 3ff3e4e

Please sign in to comment.