So that is your own build system.
Sure. It was a learning curve for me. But I wonder if other people run into the same problem.
Also, I wonder if the FreeBSD manpage should warn about different definitions of
strerror_r
being around. ? Even if I don't like those other (non-standard-compliant) definitions, Linux
is widespread.
I kind of keep repeating myself, so I'll try to be a bit more verbose now.
Yes please, sorry if I didn't understand it yet. ?
Your goal is portability to POSIX-compliant systems, then you will request the minimum POSIX version you actually need (because that way you'll still be portable to systems implementing this version but not a newer one). For strerror_r()
, #define _POSIX_C_SOURCE 200112L
is enough. If you need APIs from a newer POSIX version somewhere else in your program, you'll define a newer version instead.
I see that the manpage for
strerror_r() lists the POSIX standard versions for each function. I'll pay attention to those sections from now on.
Regarding
asprintf
, it's a bit tragic to not being able to use it in FreeBSD just because Linux doesn't let me include it without forcing me to define
_GNU_SOURCE
? It's a Linux issue, not a FreeBSD issue. ?
While
asprintf
can be worked around with your own implementation of it, there are other functions that cause the same problem which may be more problematic. I ran into this problem when using
accept4(). I'm not sure if I remember the exact problem, but I believe that when you use
accept
and when you are in a multi-threaded environment, you might leak the returned file descriptor to your child processes unless
SOCK_CLOEXEC
(see
socket(2)) is set as soon as the socket is created by
accept4
. And
accept
doesn't allow you to set this flag atomically, I think, but I don't remember the situation exactly where I needed or wanted
accept4
.
So I do believe there are other scenarios where you run into needing (or wanting?)
_GNU_SOURCE
for code that runs at least on Linux in addition to FreeBSD. Any way to get out of this trap? Do I really have to refrain from using
asprintf
,
accept4
, and possibly others? Or do I "give in" but then have to create my own
strerror_r
implementation which checks the environment / feature test macros and then internally uses whichever function is provided by the OS? It all is such a hassle. What do you think is the best practice when I need to use
strerror_r
and other functions like
accept4
. Even assuming I don't want to be very portable but only support FreeBSD and Linux, this seems to be a challenge already.
edit: Apparently, if I define
_POSIX_C_SOURCE
, I lose all the non-standard functions under FreeBSD. What is the way under FreeBSD to get them, e.g. to use
asprintf
and
strerror_r
at the same time even if
no portability is necessary? The following does not work:
Code:
#define _POSIX_C_SOURCE 200809L
#define _DEFAULT_SOURCE
But only using
_DEFAULT_SOURCE
(without
_POSIX_C_SOURCE
) seems to work. ?