使用EmBitz 编译mbed提示required from 'void WIZnet_Chip::reg_wr(uint16_t, T) [with T = short unsigned int;

来源:互联网 发布:淘宝双十一宣传片 编辑:程序博客网 时间:2024/05/02 06:45

If optimization level is >= 2 in gcc-4.1, strict-aliasing is used, and this could cause probelms when a pointer is referencing to a different type of object and the object is refered thereafter by using this pointer. That is the case in this example. So you should force the compiler to not use strict-aliasing by a argument "-fno-strict-aliasing" if you want to use "-O2" or "-O3".

就是说 optimization level>=2 默认使用 strict-aliasing, -O0和-O1就没有默认使用 -fstrict-aliasing,导致不同类型的指针指向同一内存就会提示错误。比如:

int i=1;unsigned int * p=(unsigned int *)(&i);
这样是不符合 aliasing规则的。如果使用 -O2 或 -O3 可以使用参数 -fno-strict-aliasing来避免aliasing规则。上面的代码就可以通过编译了。


使用EmBitz 编译mbed提示

.\W5500Interface\WIZnet\W5500.h|321|  required from 'void WIZnet_Chip::reg_wr(uint16_t, T) [with T = short unsigned int; uint16_t = short unsigned int]'

    template<typename T>    void reg_wr(uint16_t addr, T data) {        return reg_wr(addr, 0x04, data);    }    template<typename T>    void reg_wr(uint16_t addr, uint8_t cb, T data) {        uint8_t buf[sizeof(T)];        *reinterpret_cast<T*>(buf) = data;        for(int i = 0; i < sizeof(buf)/2; i++) { //  Little Endian to Big Endian            uint8_t t = buf[i];            buf[i] = buf[sizeof(buf)-1-i];            buf[sizeof(buf)-1-i] = t;        }        spi_write(addr, cb, buf, sizeof(buf));    }


添加 C++ Flags   -fno-strict-aliasing


0 0
原创粉丝点击