1

我正在为德州仪器的 Tiva C 系列 TMC123G Launchpad(ARM Cortex M4 MCU 板)编写一些代码,由于undefined reference to 'malloc'. startup_gcc.c 和 project.ld 是TivaWare的一部分。等效文件可以在这里找到:

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

这是我在构建时的控制台输出:

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

这是问题的一个最小示例,函数malloc中的main似乎没有引起任何问题,只有Struct_begin函数中的一个。(它被优化了。)

#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;
}
4

1 回答 1

3

您正在使用嵌入式平台。在这些情况下,GCC(或者更确切地说是 ld)可能不会自动链接到标准 C 库。您可能需要明确需要链接到平台的标准库,或者您可能需要提供自己的 malloc 实现。

于 2014-10-14T12:51:50.707 回答