• GCC Arm 11.3rel1, 12.2编译提示 _close is not implemented and will always fail


    使用GCC Arm工具链开发的项目, 在11.2下编译正常, 但是升级到 arm-gnu-toolchain-11.3.rel1 以及 arm-gnu-toolchain-12.2 之后, 编译出现警告

    /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libc_nano.a(libc_a-closer.o): in function `_close_r':
    /data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/closer.c:47: warning: _close is not implemented and will always fail
    /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libc_nano.a(libc_a-fstatr.o): in function `_fstat_r':
    /data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/fstatr.c:55: warning: _fstat is not implemented and will always fail
    /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libc_nano.a(libc_a-isattyr.o): in function `_isatty_r':
    /data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/isattyr.c:52: warning: _isatty is not implemented and will always fail
    /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/bin/ld: /opt/gcc-arm/arm-gnu-toolchain-11.3.rel1-x86_64-arm-none-eabi/bin/../lib/gcc/arm-none-eabi/11.3.1/../../../../arm-none-eabi/lib/thumb/v6-m/nofp/libc_nano.a(libc_a-lseekr.o): in function `_lseek_r':
    /data/jenkins/workspace/GNU-toolchain/arm-11/src/newlib-cygwin/newlib/libc/reent/lseekr.c:49: warning: _lseek is not implemented and will always fail
    

    这是因为使用了printf, 定义了 int _read(int file, char *ptr, int len)int _write(int file, char *ptr, int len)方法但是未定义其它方法所致.

    一个完整的输入输出重定向需要包含以下函数, 把剩下的都补齐就不会提示警告了

    int _isatty(int fd);
    int _write(int fd, char* ptr, int len);
    int _close(int fd);
    int _lseek(int fd, int ptr, int dir);
    int _read(int fd, char* ptr, int len);
    int _fstat(int fd, struct stat* st);
    

    首先引入必要的头文件

    #include <sys/stat.h>
    #include <unistd.h>
    #include <errno.h>
    

    然后是固定的方法

    
    __attribute__((weak)) int _isatty(int fd)
    {
        if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
            return 1;
    
        errno = EBADF;
        return 0;
    }
    
    __attribute__((weak)) int _close(int fd)
    {
        if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
            return 0;
    
        errno = EBADF;
        return -1;
    }
    
    __attribute__((weak)) int _lseek(int fd, int ptr, int dir)
    {
        (void)fd;
        (void)ptr;
        (void)dir;
    
        errno = EBADF;
        return -1;
    }
    
    __attribute__((weak)) int _fstat(int fd, struct stat *st)
    {
        if (fd >= STDIN_FILENO && fd <= STDERR_FILENO)
        {
            st->st_mode = S_IFCHR;
            return 0;
        }
    
        errno = EBADF;
        return 0;
    }
    

    _read 和 _write 是与底层硬件实现相关的, 需要自己实现对应的字符输入和输出

    
    __attribute__((weak)) int _read(int file, char *ptr, int len)
    {
        (void)file;
        int DataIdx;
        for (DataIdx = 0; DataIdx < len; DataIdx++)
        {
            *ptr++ = __io_getchar();
        }
        return len;
    }
    
    __attribute__((weak)) int _write(int file, char *ptr, int len)
    {
        (void)file;
        int DataIdx;
        for (DataIdx = 0; DataIdx < len; DataIdx++)
        {
            __io_putchar(*ptr++);
        }
        return len;
    }
    
  • 相关阅读:
    nginx高级玩法之根据来源ip分流
    ubuntu上的 /dev/loop0 到 /dev/loop18占到100%的处理
    nginx: [warn] conflicting server name "aaa.bbbb.com" on 0.0.0.0:80, ignored
    nginx报警:nginx: [warn] could not build optimal server_names_hash, you should increase either server_names_hash_max_size: 512 or server_names_hash_bucket_size: 64; ignoring server_names_hash_bucket_size
    nginx配置socket连接
    Syntax error: "(" unexpected shell里面的报错解决
    docker批量操作容器
    ubuntu18.04安装docker和开通对外2375端口(方便portainer管理)
    Cadence OrCad Allegro SPB 16.6 下载及安装破解指南
    关于XILINX芯片IO管脚的上拉电阻的疑问
  • 原文地址:https://www.cnblogs.com/milton/p/16755173.html
Copyright © 2020-2023  润新知