交叉c++编译器链接错误

Cross g++ compiler linker error

本文关键字:错误 链接 编译器 c++ 交叉      更新时间:2023-10-16

我正在使用Eclipse为ARM处理器交叉编译一个g++项目。我在Windows环境中使用yagarto工具链。我对C项目没有问题,但对于c++,我一直收到错误:

libc.a(lib_a-abort.o): In function `abort':
abort.c:63: undefined reference to `_exit'
libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
sbrkr.c:58 undefined reference to `_sbrk'
libc.a(lib_a-signalr.o): In function `_kill_r':
signalr.c:61: undefined reference to `_kill'

以及:

undefined reference to `_getpid'
undefined reference to `_write'
undefined reference to `_close'
undefined reference to `_fstat'
undefined reference to `_isatty'
undefined reference to `_lseek'
undefined reference to `_read'

从周围看,它看起来像我有一个链接器问题。我试着添加链接flash.ld。它来自于Atmel Studio的ARM开发工具包的GCC示例。这没有帮助。有g++的链接器吗?我还有别的问题吗?

以下是我的构建选项:

make all
Building target: Foo
Invoking: Cross G++ Linker
arm-none-eabi-g++ -nostartfiles -T C:/Users/kempsa/eclipse_workspace/Foo/flash.ld -o"Foo"  ./HALTimer1.o ./Main.o

我有源文件Main.cpp, HALTimer1.cpp和头文件Haltimer.h。主文件只包含头文件。头文件只为HALTimer定义了一个类,只有一个变量。我不认为这些会影响误差。我相信这个错误完全是由于试图在没有正确的链接器文件的情况下构建g++项目。

以下是链接器文件的内容:

/**
 * file
 *
 * brief Flash Linker script for SAM.
 *
 * Copyright (c) 2011-2012 Atmel Corporation. All rights reserved.
 *

    OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
    OUTPUT_ARCH(arm)
    SEARCH_DIR(.)
    /* Memory Spaces Definitions */
    MEMORY
    {
        rom (rx)  : ORIGIN = 0x00400000, LENGTH = 0x00040000 /* flash has two banks, one bank = 256K */
    ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00010000 /* sram, 64K */
    }
    /* The stack size used by the application. NOTE: you need to adjust  */
    __stack_size__ = DEFINED(__stack_size__) ? __stack_size__ : 0x2000;
    /* Section Definitions */
    SECTIONS
    {
        .text :
        {
            . = ALIGN(4);
            _sfixed = .;
            KEEP(*(.vectors .vectors.*))
            *(.text .text.* .gnu.linkonce.t.*)
        *(.glue_7t) *(.glue_7)
        *(.rodata .rodata* .gnu.linkonce.r.*)
        *(.ARM.extab* .gnu.linkonce.armextab.*)
        /* Support C constructors, and C destructors in both user code
           and the C library. This also provides support for C++ code. */
        . = ALIGN(4);
        KEEP(*(.init))
        . = ALIGN(4);
        __preinit_array_start = .;
        KEEP (*(.preinit_array))
        __preinit_array_end = .;
        . = ALIGN(4);
        __init_array_start = .;
        KEEP (*(SORT(.init_array.*)))
        KEEP (*(.init_array))
        __init_array_end = .;
        . = ALIGN(0x4);
        KEEP (*crtbegin.o(.ctors))
        KEEP (*(EXCLUDE_FILE (*crtend.o) .ctors))
        KEEP (*(SORT(.ctors.*)))
        KEEP (*crtend.o(.ctors))
        . = ALIGN(4);
        KEEP(*(.fini))
        . = ALIGN(4);
        __fini_array_start = .;
        KEEP (*(.fini_array))
        KEEP (*(SORT(.fini_array.*)))
        __fini_array_end = .;
        KEEP (*crtbegin.o(.dtors))
        KEEP (*(EXCLUDE_FILE (*crtend.o) .dtors))
        KEEP (*(SORT(.dtors.*)))
        KEEP (*crtend.o(.dtors))
        . = ALIGN(4);
        _efixed = .;            /* End of text section */
    } > rom
    /* .ARM.exidx is sorted, so has to go in its own output section.  */
    PROVIDE_HIDDEN (__exidx_start = .);
    .ARM.exidx :
    {
      *(.ARM.exidx* .gnu.linkonce.armexidx.*)
    } > rom
    PROVIDE_HIDDEN (__exidx_end = .);
    . = ALIGN(4);
    _etext = .;
    .relocate : AT (_etext)
    {
        . = ALIGN(4);
        _srelocate = .;
        *(.ramfunc .ramfunc.*);
        *(.data .data.*);
        . = ALIGN(4);
        _erelocate = .;
    } > ram
    /* .bss section which is used for uninitialized data */
    .bss (NOLOAD) :
    {
        . = ALIGN(4);
        _sbss = . ;
        _szero = .;
        *(.bss .bss.*)
        *(COMMON)
        . = ALIGN(4);
        _ebss = . ;
        _ezero = .;
    } > ram
    /* stack section */
    .stack (NOLOAD):
    {
        . = ALIGN(8);
          _sstack = .;
        . = . + __stack_size__;
        . = ALIGN(8);
        _estack = .;
    } > ram
    . = ALIGN(4);
    _end = . ;
}

编译器完整输出:

Building target: Foo
Invoking: Cross G++ Linker
arm-none-eabi-g++ -nostartfiles -T C:/Users/kempsa/eclipse_workspace/Foo/flash.ld -o"Foo"      ./HALTimer.o ./Main.o
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-abort.o): In function `abort':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcstdlib/../../../../../newlib-1.20.0/newlib/libc/stdlib/abort.c:63: undefined reference to `_exit'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-sbrkr.o): In function `_sbrk_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/sbrkr.c:58: undefined reference to `_sbrk'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-signalr.o): In function `_kill_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/signalr.c:61: undefined reference to `_kill'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-signalr.o): In function `_getpid_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/signalr.c:96: undefined reference to `_getpid'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-writer.o): In function `_write_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/writer.c:58: undefined reference to `_write'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-closer.o): In function `_close_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/closer.c:53: undefined reference to `_close'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-fstatr.o): In function `_fstat_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/fstatr.c:62: undefined reference to `_fstat'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-isattyr.o): In function `_isatty_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/isattyr.c:58: undefined reference to `_isatty'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-lseekr.o): In function `_lseek_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/lseekr.c:58: undefined reference to `_lseek'
c:/yagarto/bin/../lib/gcc/arm-none-eabi/4.7.2/../../../../arm-none-eabi/liblibc.a(lib_a-readr.o): In function `_read_r':
C:msys1.0homeyagartonewlib-buildarm-none-eabinewliblibcreent/../../../../../newlib-1.20.0/newlib/libc/reent/readr.c:58: undefined reference to `_read'
collect2.exe: error: ld returned 1 exit status
make: *** [Foo] Error 1

添加链接器选项-Wl,——gc-sections,问题就解决了

相关文章: