1

Texas Instruments の 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

これは問題の最小限の例です。関数内のは問題を引き起こさないようで、関数内の 1 つだけmallocです。mainStruct_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 に答える