跨 g++ 编译器链接器错误

Posted

技术标签:

【中文标题】跨 g++ 编译器链接器错误【英文标题】:Cross g++ compiler linker error 【发布时间】:2013-01-10 19:45:13 【问题描述】:

我正在使用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

【问题讨论】:

好的,所以,如果您认为是链接器文件问题,那么请列出您的C:/Users/kempsa/eclipse_workspace/Foo/flash.ld 链接器命令文件的内容。 用 gcc 和 g++ 试试-v 选项,看看有什么不同。 通过objdump -af *.o *.a检查它们来验证您尝试链接在一起的对象和库是否适用于相同的体系结构@ 我无法让 objdump -af *.o *.a 正常工作...但是链接器文件是为 gcc 构建的。有没有办法把它移植到g+?我一直无法找到仅使用 g++ gcc 的示例项目。是否有我可以修改以适合我的项目的 g++ 通用链接器文件? 【参考方案1】:

添加链接器选项 -Wl,--gc-sections 应该可以解决问题

【讨论】:

您不应将此作为答案发布 - 而是将其包含在您的问题中。现在为您完成。

以上是关于跨 g++ 编译器链接器错误的主要内容,如果未能解决你的问题,请参考以下文章

链接器错误,使用 g++ 链接到由 gcc 编译的库,未定义对该函数的引用

MinGW 链接器错误:winsock

gtkmm 链接器错误

链接器错误:未定义的函数引用

未定义对向量<String> g++ 链接器错误的引用[重复]

编译FFMPEG时Mac上的g ++链接错误