Search code examples
ctypescompiler-errorsarmiar

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.

As from CCS I can not flash the firmware permanently, I'm working with IAR to develop this action.

On IAR, I have created the workspace, the project and included all libraries and files needed to compile the firmware. 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 *"

    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 *"

    int16_t *accData[3] = malloc(sizeof(int16_t));
    
  • Error[Pe120]: return value type ("signed short **") does not match the function type ("signed short*")

    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!


Solution

  • Which is the root cause of these errors?

    "a value of type "int"" is the root cause. There should be no int here! Just the signed short* (which is your int16_t*) and a void* from malloc.

    This is because you are using a C90 compiler and forgot to #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"". But malloc actually returns a void*, so this is a severe bug. Solve this by including the header stdlib.h where malloc is found.

    This undesired and irrational behavior of the language was fixed 17 years ago. Consider using a modern compiler instead, or configure your compiler to use the current C language standard (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]) );