build/cmake/stdlib: guard include/cxx by LIBMINIABI, fix div_t conflict

include/cxx contains NuttX's mini C++ ABI shims and must only be added
to the include path when CONFIG_LIBMINIABI is selected.  tools/Config.mk
was adding it unconditionally for every non-LIBCXX/non-UCLIBCXX build,
and the platform.cmake files for arm, arm64, risc-v, x86_64 and tricore
were adding it inside the CONFIG_LIBCXXTOOLCHAIN block.

With an unpatched downloaded ARM GNU Toolchain, <cstdlib> uses
newlib's stdlib.h, defining div_t as an anonymous struct.  A later
inclusion of NuttX's stdlib.h via <cstdio>->stdio.h->kmalloc.h then
redefines div_t with struct tag div_s, causing a conflicting declaration
error.

Guard the div_t/ldiv_t/lldiv_t definitions in stdlib.h with
redefinitions when a toolchain stdlib.h was already processed.

Also fix lldiv_s members typed as long instead of long long.

Signed-off-by: trns1997 <trns1997@gmail.com>
This commit is contained in:
trns1997
2026-03-28 13:50:35 +01:00
committed by Alan C. Assis
parent bab7e3e51b
commit 219a5ce09e
7 changed files with 36 additions and 4 deletions
+19 -3
View File
@@ -87,8 +87,23 @@
* Public Type Definitions
****************************************************************************/
/* Structure type returned by the div() function. */
/* Structure type returned by the div() function.
*
* When CONFIG_LIBCXXTOOLCHAIN is active the toolchain's <cstdlib> uses
* #include_next <stdlib.h> which bypasses this file and lands on the
* toolchain's own stdlib.h (e.g. newlib's _STDLIB_H_ guard). That file
* defines div_t/ldiv_t/lldiv_t with an anonymous struct that GCC internally
* names struct div_t. A subsequent inclusion of this file (through the
* cstdio -> stdio.h -> kmalloc.h chain) would then attempt to redefine the
* same typedef with a different struct tag, causing a "conflicting
* declaration" error. Guard against that by skipping our own struct/typedef
* definitions when a toolchain stdlib.h has already been included.
*
* _STDLIB_H_ - newlib (ARM GNU Toolchain, picolibc, avr-libc, ...)
* _STDLIB_H - glibc / musl (native host toolchains)
*/
#if !defined(_STDLIB_H_) && !defined(_STDLIB_H)
struct div_s
{
int quot; /* Quotient */
@@ -111,11 +126,12 @@ typedef struct ldiv_s ldiv_t;
struct lldiv_s
{
long quot; /* Quotient */
long rem; /* Remainder */
long long quot; /* Quotient */
long long rem; /* Remainder */
};
typedef struct lldiv_s lldiv_t;
#endif /* !defined(_STDLIB_H_) && !defined(_STDLIB_H) */
/****************************************************************************
* Public Function Prototypes