Based on my cmake pull-request(1) to fix the cmake build on OpenBSD, the
following question has arisen which is worth analysing?

"It seems OpenBSD has a strange behavior because macro _POSIX_C_SOURCE is a
standard!  @sizeofvoid What are the errors raised if _POSIX_C_SOURCE or
_XOPEN_SOURCE are defined?" -- Marc Chevrier

[1]: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/6000

The following code includes the if-defs from cmake with a simple sstream
include.

$ cat define_test.cxx
#if !defined(_WIN32) && !defined(__sun)
// POSIX APIs are needed
// NOLINTNEXTLINE(bugprone-reserved-identifier)
#  define _POSIX_C_SOURCE 200809L
#endif
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
// For isascii
// NOLINTNEXTLINE(bugprone-reserved-identifier)
#  define _XOPEN_SOURCE 700
#endif

#include <sstream>
int main () { return 0; }

$ clang++ -std=c++17 define_test.cxx  # also with c++11/14
In file included from define_test.cxx:16:
In file included from /usr/include/c++/v1/sstream:173:
In file included from /usr/include/c++/v1/ostream:140:
In file included from /usr/include/c++/v1/locale:207:
/usr/include/c++/v1/__bsd_locale_fallbacks.h:122:17: error: use of undeclared 
identifier 'vasprintf'; did you mean 'vsprintf'?
    int __res = vasprintf(__s, __format, __va);
                ^
/usr/include/c++/v1/cstdio:124:9: note: 'vsprintf' declared here
using ::vsprintf;
        ^
In file included from define_test.cxx:16:
In file included from /usr/include/c++/v1/sstream:173:
In file included from /usr/include/c++/v1/ostream:140:
In file included from /usr/include/c++/v1/locale:207:
/usr/include/c++/v1/__bsd_locale_fallbacks.h:122:27: error: cannot initialize a 
parameter of type 'char *' with an lvalue of type 'char **'
    int __res = vasprintf(__s, __format, __va);
                          ^~~
/usr/include/stdio.h:269:21: note: passing argument to parameter here
int      vsprintf(char *, const char *, __va_list);
                        ^
2 errors generated

Looks like, if "_XOPEN_SOURCE 700" or "_POSIX_C_SOURCE 200809L" is defined we
run in this compile error. The question is, is that deliberate?

Reply via email to