1

I am using (STM32F407VG-Discovey board) with compiler "gcc-arm-none-eabi-7-2017-q4-major" (arm-none-eabi-gcc) and I am trying to implement "google project flatbuffers". That needs for running time library malloc.h, and also heap memory.

I turn on heap memory on my ARM processor and tested it with include and and try basic operation calling malloc function. All works fine.

Now I include google flatbuffers header files and now I get error "undefined reference to `posix_memalign'". My linker can't find this function. It doesn't find but it should already have it posix_memalign in stdlib.h Error looks like that:

enter image description here

In my CMake file I have set my flags to

SET(CMAKE_C_FLAGS "-mthumb -fno-builtin -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -Wall -std=gnu11 -ffunction-sections -fdata-sections -fomit-frame-pointer -mabi=aapcs -fno-unroll-loops -ffast-math -ftree-vectorize -lc -lrdimon" CACHE INTERNAL "c compiler flags")

Also I figure out, if I don't use flag -lc and -lrdimo, there will be undefined reference to _write(), _read(), _sbrk, _exit .....

Explanation why this is not duplicate: I know adding, linker library with CMake you execute command target_link_libraries(). Problem here is that for non trivial reason my liner will not find posix_memalign function. But it will find other functions like malloc, alloc, free, ... They all are in "stdlib.h".

user45189
  • 169
  • 1
  • 9
  • No this is not duplicate at all. I know you can call target_link_libraries() and add flags to linker this way, but is not necessary. But this is not a problem I try to solve. – user45189 Dec 05 '18 at 10:36
  • It could be issue with ordering libraries in the linker's invocation command line. If you know that `CMAKE_C_FLAGS` is not a proper way for link libraries, then why do you use it in your code and show us that code? – Tsyvarev Dec 05 '18 at 12:21
  • Can I ask why I did get negative votes on my question. I know it is to specific and kinda not clear question, but still, why? – user45189 Dec 05 '18 at 12:51

3 Answers3

1

Try to use

#include <malloc.h>
    void* p;
    p = memalign(alignment, size);

instead of

    posix_memalign(&p, alignment, size);

See also this link

0

Using -std=c99 was not a viable solution for our team because we have C11 code.

A better solution is to get flatcc to use the generic aligned alloc functions provided by flatcc instead of the posix ones.

In your build process, make sure that -DFLATCC_USE_GENERIC_ALIGNED_ALLOC is passed to your compilation command line to activate these generic versions.

Defining FLATCC_USE_GENERIC_ALIGNED_ALLOC activates the generic implementations of aligned allocations in include/flatcc/flatcc_alloc.c.

-1

At ARM ToolChain official site under section 6.5.5. Alignment of C heap storage, it said for usage of function `posix_memalign´ you must use standard C99 not C11 as I have set.

So you must add in your CMAKE_C_FLAGS this flag: -std=c99

If you have set flag -std=c11 you should remove it.

user45189
  • 169
  • 1
  • 9