mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Nicholas Fraser <nick@ludocode.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] Detect qsort_r() support with preprocessor
Date: Tue, 19 Apr 2022 09:32:12 -0400	[thread overview]
Message-ID: <20220419133211.GA7074@brightrain.aerifal.cx> (raw)
In-Reply-To: <823acf0f-461d-d599-feb7-e0e21cfeb51d@ludocode.com>

On Mon, Apr 18, 2022 at 11:38:49PM -0400, Nicholas Fraser wrote:
> Just to recap: I'm writing a header-only library. My intention is for users to
> just put the headers on their include path and use it, no configuration needed.
> I'd like to use any variation of qsort_r()/qsort_s() that's available on the
> platform.
> 
> GNU/musl/POSIX-next qsort_r() and C11 Annex K qsort_s() are virtually the
> same; Free/Net/DragonFlyBSD and Apple qsort_r() are the same; and Windows
> qsort_s() is the outlier. There are only three possible prototypes so it's
> trivial for my code to permute the arguments to match what's available. All I
> need to do is detect which one the platform has.

This would be a great usage case for the "Macro-based advertisement of
libc extensions" proposal on libc-coord. The right thing to do here is
to try to move that forward, explaining how it solves your problem.
I'll try to revitalize that.

> In cases where the headers declare it conditionally, I simply declare it myself
> so that my users don't need to define _GNU_SOURCE or __STDC_WANT_LIB_EXT1__ or
> whatever:

You cannot #define _GNU_SOURCE or __STDC_WANT_LIB_EXT1__ after any
standard header has already been included. The only place they can
safely be defined is at the top of the source file before any headers,
or on the command line with -D. There are two reasons for this: one is
that the rules say so (both POSIX and glibc document this, in addition
to musl). The other more mechanical reason is that the header you're
trying to get the definitions from might already have been included
(directly or indirectly) earlier, whereby the multiple-inclusion guard
prevents it from exposing anything else the second time it's included.
This extends to the case where the FTMs are actually processed by a
unified file (like features.h) into internal-use-only macros on the
first inclusion of any standard heaader. For this reason, it's
possible that even headers which weren't already included once will
still fail to see the updated FTMs.

So, defining these from a header-only library is an on-starter unless
you impose a requirement on the caller that they must include your
header absolute-first (and then they can only use one such header-only
library). An approach that actually works is just documenting that the
caller must ensure they're defined appropriately for features they
want your library to be able to use. Or, just being compatible with
whatever choice the caller made (so using your own fallback or
whatever if it's not exposed).

Rich

      parent reply	other threads:[~2022-04-19 13:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-16  8:13 Nicholas Fraser
2022-04-16 12:01 ` Markus Wichmann
2022-04-16 14:16   ` Quentin Rameau
2022-04-16 17:50     ` Nicholas Fraser
2022-04-16 18:42       ` Quentin Rameau
2022-04-16 23:59         ` Nicholas Fraser
2022-04-17  2:04       ` Rich Felker
2022-04-19  3:38         ` Nicholas Fraser
2022-04-19  6:59           ` Markus Wichmann
2022-04-19 23:10             ` Nicholas Fraser
2022-04-19 13:32           ` Rich Felker [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220419133211.GA7074@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    --cc=nick@ludocode.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).