Problem building libstdc++ for the avr target

Vladimir V vv.os.swe@gmail.com
Thu Mar 25 21:37:51 GMT 2021


Thank you for the quick reply.

I am wondering if the freestanding libstdc++ requires that only
freestanding subset of the underlying libc is used?
avr-libc has stdlib.h with malloc declaration but it is not 'visible' in
this code. I was thinking that maybe something like that
would work (works in my case):

diff --git a/libstdc++-v3/include/c_global/cstdlib
b/libstdc++-v3/include/c_global/cstdlib
index 47b954cf2..f04fd08de 100644
--- a/libstdc++-v3/include/c_global/cstdlib
+++ b/libstdc++-v3/include/c_global/cstdlib
@@ -43,7 +43,8 @@
 #ifndef _GLIBCXX_CSTDLIB
 #define _GLIBCXX_CSTDLIB 1

-#if !_GLIBCXX_HOSTED
+#if !_GLIBCXX_HOSTED && !_GLIBCXX_HAVE_STDLIB_H
+
 // The C standard does not require a freestanding implementation to
 // provide <stdlib.h>.  However, the C++ standard does still require
 // <cstdlib> -- but only the functionality mentioned in

It includes the stdlib.h from target libc if it is present.


чт, 25 мар. 2021 г. в 19:27, Jonathan Wakely <jwakely@redhat.com>:

> On 25/03/21 12:36 +0000, Jonathan Wakely wrote:
> >On 25/03/21 10:27 +0100, Vladimir V wrote:
> >>Hello.
> >>
> >>Recently I tried the 'freestanding' build of the libstdc++ for the avr to
> >>verify that my fixes were sufficient for it as well.
> >>Unfortunately, I encountered the following error:
> >>../../../../libstdc++-v3/libsupc++/new_opa.cc: In function 'void*
> >>__gnu_cxx::aligned_alloc(std::size_t, std::size_t)':
> >>../../../../libstdc++-v3/libsupc++/new_opa.cc:97:28: error: 'malloc' was
> >>not declared in this scope
> >>  97 |   void* const malloc_ptr = malloc(sz + al);
> >>
> >>My investigation showed that in the chain of includes the stdlib.h of the
> >>avr-libc is not present.
> >>It looks like includes are resolved in the following way (here are the
> >>original paths as they are linked from out folders during build):
> >>new_opa.cc -> libstdc++-v3/include/c_compatibility/stdlib.h ->
> >>libstdc++-v3/include/c_global/cstdlib
> >>In cstdlib, for non-hosted builds stdlib.h is not included.
> >>
> >>This problem is not present if the non-hosted build is done with
> >>'with-newlib' flag instead of 'with-avrlibc' (common workaround as the
> >>avrlibc build was fixed only recently).
> >>Might it be that some include order depends on the target libc? Or maybe
> >>some implicit conditions are involved?
> >>
> >>Could you please support me with this issue?
> >
> >I think this is needed:
> >
> >--- a/libstdc++-v3/libsupc++/new_opa.cc
> >+++ b/libstdc++-v3/libsupc++/new_opa.cc
> >@@ -54,6 +54,10 @@ extern "C"
> >   void *posix_memalign(void **, size_t alignment, size_t size);
> > # elif _GLIBCXX_HAVE_MEMALIGN
> >   void *memalign(size_t alignment, size_t size);
> >+#else
> >+  // A freestanding C runtime may not provide "malloc" -- but there is no
> >+  // other reasonable way to implement "operator new".
> >+  void *malloc (std::size_t);   with GCC 7.5
>
> Oops, there was a stray "with GCC 7.5" that got into my clipboard
> there!
>
> I've pushed the attached patch, please test and confirm if it fixes
> your problem.
>
>
>


More information about the Libstdc++ mailing list