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

标签 c++ c arm eclipse-cdt atmel

我正在使用 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/lib\libc.a(lib_a-abort.o): In function `abort':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\stdlib/../../../../../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/lib\libc.a(lib_a-sbrkr.o): In function `_sbrk_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-signalr.o): In function `_kill_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-signalr.o): In function `_getpid_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-writer.o): In function `_write_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-closer.o): In function `_close_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-fstatr.o): In function `_fstat_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-isattyr.o): In function `_isatty_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-lseekr.o): In function `_lseek_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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/lib\libc.a(lib_a-readr.o): In function `_read_r':
C:\msys\1.0\home\yagarto\newlib-build\arm-none-eabi\newlib\libc\reent/../../../../../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,问题应该解决

关于c++ - 交叉 g++ 编译器链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14266012/

相关文章:

c - 所有输入函数都使用相同的缓冲区吗?

c - 逐行读取文件并在 C 中使用 strtok()

c - 在 Opencl 中 clGetDeviceIDs 返回错误 CL_OUT_OF_HOST_MEMORY

objective-c - 用于计算平均标准偏差的 Arm 循环代码优化

c++ - ARM 上的预取指令

c++ - 属性可以应用于构造函数参数吗?

c++ - 使用 C++(无 winapi)将 UTF-8 字符转换为最接近的等效 ASCII 字符

c++ - Boost Serialization - 导出多个 CPP 文件

c++ - 如何将 char[] 转换为 std::string

arrays - 如何在 C 中检查整数指针数组是否为 NULL?