1

I'm working on some code for Texas Instruments' Tiva C Series TMC123G Launchpad (an ARM Cortex M4 MCU board), my code doesn't compile due to a undefined reference to 'malloc'. startup_gcc.c and project.ld are part of TivaWare. Equivalent files can be found here:

  • /src/startup_gcc.c
  • /TM4C123GH6PM.ld

Here my console output when building:

arm-none-eabi-gcc -o build/minimal.o src/minimal.c -g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -Os -ffunction-sections -fdata-sections -MD -std=c99 -Wall -pedantic -DPART_TM4C123GH6PM -c -DTARGET_IS_BLIZZARD_RA1 -Dgcc -DF_CPU=80000000L -Isrc -Ilibraries -I/home/jakob/opt/tivaware
arm-none-eabi-gcc -o build/startup_gcc.o /home/jakob/opt/tivaware/examples/project/startup_gcc.c -g -mthumb -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=softfp -Os -ffunction-sections -fdata-sections -MD -std=c99 -Wall -pedantic -DPART_TM4C123GH6PM -c -DTARGET_IS_BLIZZARD_RA1 -Dgcc - DF_CPU=80000000L -Isrc -Ilibraries -I/home/jakob/opt/tivaware
arm-none-eabi-ld -o build/a.out build/minimal.o build/startup_gcc.o -T /home/jakob/opt/tivaware/examples/project/project.ld --entry ResetISR --gc-sections
build/minimal.o: In function `Struct_begin':
/home/jakob/TivaMallocProblemMini/src/minimal.c:29: undefined reference to `malloc'
Makefile:66: recipe for target 'build/a.out' failed
make: *** [build/a.out] Error 1

Here is a minimal example of the problem, the malloc in the main function does not seem to cause any problems, only the one in the Struct_begin function. (It was optimized away.)

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>

typedef struct s_Struct
{
    int16_t param;
} Struct;

Struct* Struct_begin(int16_t param);

int main() {
    Struct* s;

    s = malloc(sizeof(Struct));
    free(s);

    s = Struct_begin(10);
    s->param=0;

    for(;;);

    return EXIT_SUCCESS;
}

Struct* Struct_begin(int16_t param) {
    Struct* s;

    s = malloc(sizeof(Struct));
    s->param = param;

    return s;
}
Jakob Klepp
  • 15
  • 1
  • 6
  • https://github.com/dwelch67/tivac_launchpad has some bare metal examples for the tiva c launchpads. Not sure what problem you are really trying to solve here (why would you ever malloc on a microcontroller) but these very simple examples provide makefiles and such to get a working binary for these microcontrollers. – old_timer Oct 14 '14 at 13:58
  • Is there a better way to get a pointer on a struct, always happy to learn more. – Jakob Klepp Oct 14 '14 at 16:14
  • just declare the struct not a pointer to it, this is embedded bare metal code not an operating system – old_timer Oct 14 '14 at 19:48
  • Struct my_struct; //done, allocated – old_timer Oct 14 '14 at 19:49
  • Oh, that would make it a easier. – Jakob Klepp Oct 15 '14 at 10:48
  • or you could do some sort of *mystruct definition then in the code mystruct=(Struct *)0x1234500; and pick the address you want it at or a number of other things, but simply declaring it and depending on global or local it is automatically allocated from .data or the stack, no mallocs – old_timer Oct 15 '14 at 20:20

1 Answers1

3

You are working on an embedded platform. In these circumstances, GCC (or rather ld) may not automatically link to a standard C library. You might need to explicitly need to link to the platform's standard library or you might need to supply your own implementation of malloc.

doron
  • 27,972
  • 12
  • 65
  • 103
  • that is in part what newlib is about, a C library that is easy to port to whatever, there are a couple of backend files libc_func.c and another one in the same directory, which you port to your environment. It is super easy for starting out to just go through and make all the functions return success, and then you can implement an inefficient but functional malloc (sbrk) by starting with a heap base you define and just keep adding to it and returning that as the address... – old_timer Oct 14 '14 at 13:54