简体   繁体   English

IAR编译失败,CCS编译成功。 类型不兼容

[英]IAR compilation failure, CCS compilation works. Types Incompatibility

After developing a new firmware (main and libraries) with CCS for my CC2538, all errors are debugged, and now, device is working fine. 用CCS为我的CC2538开发新的固件(主固件和库)后,所有错误均已调试,现在设备可以正常工作。

As from CCS I can not flash the firmware permanently, I'm working with IAR to develop this action. 从CCS开始,我无法永久刷新固件,因此我正在与IAR合作开发此操作。

On IAR, I have created the workspace, the project and included all libraries and files needed to compile the firmware. 在IAR上,我创建了工作区,项目,并包含了编译固件所需的所有库和文件。 But, compilation fails due to incompatible types errors. 但是,由于类型不兼容错误,编译失败。

  • Error[Pe144]: a value of type "int" cannot be used to initialize an entity of type "signed short *" 错误[P​​e144]:“ int”类型的值不能用于初始化“ signed short *”类型的实体

     int16_t *accData[3] = malloc(sizeof(int16_t)); 
  • Error[Pe513]: a value of type "int" cannot be assigned to an entity of type "signed short *" 错误[P​​e513]:无法将类型为“ int”的值分配给类型为“ signed short *”的实体

     int16_t *accData[3] = malloc(sizeof(int16_t)); 
  • Error[Pe120]: return value type ("signed short **") does not match the function type ("signed short*") 错误[P​​e120]:返回值类型(“带符号的短符号**”)与功能类型(“带符号的短符号*”)不匹配

     int16_t * lsm303d_readAccData(void) { int16_t *accData[3] = malloc(sizeof(int16_t)); ... return accData; } 

Which is the root cause of these errors? 这些错误的根本原因是什么? Maybe, any option of the compiler? 也许,编译器有什么选择吗? Do I need to add any file? 我需要添加任何文件吗? Or prototype on the code? 还是原型上的代码?

KR! KR!

The first error is somewhat misleading. 第一个错误有些令人误解。 It seems to indicate that you forgot to include <stdlib.h> , so malloc is undefined and the compiler assumes it returns int . 似乎表明您忘记了包括<stdlib.h> ,因此malloc是未定义的,编译器假定它返回int

In any case, you are assigning a pointer to an array: this is incorrect. 无论如何,您都将分配一个指向数组的指针:这是不正确的。

Returning the address of a local automatic array is incorrect too. 返回本地自动数组的地址也不正确。

You should define accData as a pointer instead of an array, and make it point to an allocated array of int16_t . 您应该将accData定义为指针而不是数组,并使其指向分配的int16_t数组。 You seem to want this array to hold 3 elements, otherwise modify the code accordingly: 您似乎希望此数组包含3元素,否则相应地修改代码:

#include <stdlib.h>

int16_t *lsm303d_readAccData(void) {
    int16_t *accData = malloc(sizeof(int16_t) * 3);
    ...
    return accData;
}

You should configure the compiler to issue more warnings and refuse obsolete constructions such as implicit int. 您应该配置编译器以发出更多警告并拒绝过时的构造,例如隐式int。 For gcc , add -std=c99 or -std=c11 and -Wall -Wextra -Werror . 对于gcc ,添加-std=c99-std=c11-Wall -Wextra -Werror

Which is the root cause of these errors? 这些错误的根本原因是什么?

"a value of type "int"" is the root cause. 根本原因是“类型为“ int”的值”。 There should be no int here! 这里应该没有int Just the signed short* (which is your int16_t*) and a void* from malloc. 只是带signed short* (这是您的int16_t *)和来自malloc的void*

This is because you are using a C90 compiler and forgot to #include <stdlib.h> . 这是因为您使用的是C90编译器,却忘记了#include <stdlib.h> Upon finding a function with no prototype, C90 would implicitly assume you want a function returning int, which explains the compiler errors "a value of type "int"". 找到没有原型的函数后,C90会隐式假定您要一个返回int的函数,这说明了编译器错误“类型为“ int”的值”。 But malloc actually returns a void* , so this is a severe bug. 但是malloc实际上返回一个void* ,因此这是一个严重的错误。 Solve this by including the header stdlib.h where malloc is found. 通过在其中找到malloc的头文件stdlib.h解决此问题。

This undesired and irrational behavior of the language was fixed 17 years ago. 这种不希望的,不合理的语言行为在17年前已得到解决。 Consider using a modern compiler instead, or configure your compiler to use the current C language standard (ISO 9899:2011). 考虑改用现代编译器,或将编译器配置为使用当前的C语言标准(ISO 9899:2011)。

That being said, this code doesn't make any sense either: 话虽这么说,这段代码也没有任何意义:

int16_t *accData[3] = malloc(sizeof(int16_t));

You probably meant 你可能是说

int16_t *accData = malloc( sizeof(int16_t[3]) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM