0
\$\begingroup\$

I need some help to solve this error. I am new to use e2 studio of Renesas. I started to import and build a project, which used for GR-Peach board. Then I got this error:

error log : undefined reference to 'posix_memalign'

My toolchain version is 8.3.1.20190703. And the file definition is stdlib.h in directory C:\Program Files (x86)\GNU Tools ARM Embedded\8 2019-q3-update\arm-none-eabi\include

#if __POSIX_VISIBLE >= 200112
int posix_memalign (void **, size_t, size_t) __nonnull((1))
        __result_use_check;
#endif

I also tried changing -std=gnu99 and using aligned_alloc instead for posix_memalign but they not worked.

// definition in stdlib.h
/*
 * If we're in a mode greater than C99, expose C11 functions.
 */
#if __ISO_C_VISIBLE >= 2011
void *  aligned_alloc(size_t, size_t) __malloc_like __alloc_align(1)
        __alloc_size(2) __result_use_check;
int at_quick_exit(void (*)(void));
_Noreturn void
    quick_exit(int);
#endif /* __ISO_C_VISIBLE >= 2011 */
\$\endgroup\$
1
  • \$\begingroup\$ If possible, it is better to include the error as text, rather than a screenshot, as text is searchable \$\endgroup\$ Nov 15 '19 at 8:21
0
\$\begingroup\$

[UPDATE] The error log as below

'Building target: USBHostC270_example_GR-PEACH.elf'
'Invoking: Cross ARM C++ Linker'
arm-none-eabi-g++ @"USBHostC270_example_GR-PEACH.elf.in"
c:/program files (x86)/gnu tools arm embedded/8 2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld.exe: ./USBHost_custom_Addiso/USBisochronous/USBIsochronous.o: in function `HCITD::operator new(unsigned int, int)':
C:\Users\HoangSHC\e2_studio\workspace\USBHostC270_example_GR-PEACH\Debug/../USBHost_custom_Addiso/USBisochronous/USBIsochronous.h:41: undefined reference to `posix_memalign'
c:/program files (x86)/gnu tools arm embedded/8 2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld.exe: C:\Users\HoangSHC\e2_studio\workspace\USBHostC270_example_GR-PEACH\Debug/../USBHost_custom_Addiso/USBisochronous/USBIsochronous.h:42: undefined reference to `posix_memalign'
c:/program files (x86)/gnu tools arm embedded/8 2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld.exe: ./USBHost_custom_Addiso/USBisochronous/USBIsochronous.o: in function `_HCED::operator new(unsigned int)':
C:\Users\HoangSHC\e2_studio\workspace\USBHostC270_example_GR-PEACH\Debug/../USBHost_custom_Addiso/USBisochronous/USBIsochronous.h:99: undefined reference to `posix_memalign'
collect2.exe: error: ld returned 1 exit status
make: *** [USBHostC270_example_GR-PEACH.elf] Error 1
makefile:125: recipe for target 'USBHostC270_example_GR-PEACH.elf' failed

[SOLVE] Origin source code

        void* p;
        if (posix_memalign(&p, 256, size) == 0) {
            return p;
        }
        return NULL;

Replace to memalign

        void* p;
        p = memalign(256, size);
        return p;

For details

    inline void* operator new(size_t size) {
        void* p;
#if !defined (__CC_ARM) && (!defined (_POSIX_C_SOURCE) || (_POSIX_C_SOURCE >= 200112L))
        p = memalign(256, size);
        return p;
#else
        if (posix_memalign(&p, 256, size) == 0) {
            return p;
        }
        return NULL;
#endif
    }

After changing, I got this error: undefined reference to `__wrap__memalign_r'

'Building target: USBHostC270_example_GR-PEACH.elf'
'Invoking: Cross ARM C++ Linker'
arm-none-eabi-g++ @"USBHostC270_example_GR-PEACH.elf.in"
c:/program files (x86)/gnu tools arm embedded/8 2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld.exe: c:/program files (x86)/gnu tools arm embedded/8 2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/lib/thumb/v7+fp/hard\libc.a(lib_a-malign.o): in function `memalign':
malign.c:(.text.memalign+0xe): undefined reference to `__wrap__memalign_r'
collect2.exe: error: ld returned 1 exit status
make: *** [USBHostC270_example_GR-PEACH.elf] Error 1
makefile:125: recipe for target 'USBHostC270_example_GR-PEACH.elf' failed

Then followed this

to solve the problem.

For E2 Studio right click to project > Properties > C/C++ Build > Settings > Cross ARM C++ Linker > Miscellaneous > Linker flags (-Xlinker [option])

Linker flags (-Xlinker [option])

--wrap=_malloc_r
--wrap=_free_r
--wrap=_realloc_r
--wrap=_memalign_r
--wrap=_calloc_r

adding '_' symbol before the flags

--wrap=__malloc_r
--wrap=__free_r
--wrap=__realloc_r
--wrap=__memalign_r
--wrap=__calloc_r

[RESEARCH]

After comparing stdlib.h between older version 4.9 (4.9 2015q3) and current version 8 (8 2019-q3-update). I thought that posix_memalign maybe not supported anymore

In version 8

arm-none-eabi/include/stdlib.h (not support posix_memalign)

#if __POSIX_VISIBLE >= 200112
int posix_memalign (void **, size_t, size_t) __nonnull((1))
        __result_use_check;
#endif

arm-none-eabi/include/sys/features.h

#define _POSIX_C_SOURCE     200809L

In version 4.9

arm-none-eabi/include/stdlib.h (support posix_memalign)

#ifdef __rtems__
int _EXFUN(posix_memalign,(void **, size_t, size_t));
#endif

The GNU page says

9.637 posix_memalign

Gnulib module: —

Portability problems not fixed by Gnulib:

This function is missing on some platforms: Mac OS X 10.5, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin 1.5.x, mingw, MSVC 14, Interix 3.5, BeOS, Android 4.1.

Read more in

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.