This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Conditional compilation

Hi,

Is it possible to do conditional compilation for a structure in TI C/C++ compiler.

If not true for condition I want the compiler to show error during compilation. For example

For number of bytes in a structure

#if ((sizeof(structure)& 0x03) != 0)

#if (sizeof(structure)& 0x03)

For some byte aligned

#if ((&structure)& 0x03)

 

Regads,

Jeeva

  • The "sizeof" operator and "&" operator are part of the compiler and not available in the preprocessor. Usually you need to do such tests at runtime. I am not an expert with the TI compiler. There might be some non-standard extensions.

    If you trying to ensure a structure is not padded between members, most compilers have a "packed" pragma. There are many reasons not to pack a structure but it seems to be commonly used as a quick way to deal with the issue.

    Similarly with 4 byte alignment. Most compilers have alignment pragmas. The work-around, that sometimes works, would be allocate your memory as an array of longs (assuming a 32 bit "int").

     

  • Norman Wong said:
    The "sizeof" operator and "&" operator are part of the compiler and not available in the preprocessor. Usually you need to do such tests at runtime. I am not an expert with the TI compiler. There might be some non-standard extensions.

    The TI compiler has no extensions which support evaluating sizeof() in the preprocessor.

    Thanks and regards,

    -George

  • Hi George,

    As of now Im using below code


    e.g.

    line#1       if((sizeof(structure_1)&0x7)!=0)
    line#2       {
    line#3           return;
    line#4       }
    line#5       structure_1[2].element1 = value;
    line#6      .............................


    In Linux compiler, it is giving only warning as  "line 5: warning #112-D: statement is unreachable" if the IF condition fails, i.e. if the structure_1 size is not multiple of 8 bytes. Is there a way to make this as error or stop the compilation at that point. So that it will catch the developers attention to fix this issue. Or can you tell me how this compiler gets the size of the structure.

    Regards,

    Jeeva

  • jeevarathnam kuppusamy said:
    Is there a way to make this as error or stop the compilation at that point.

    Yes.  Either add

    #pragma diag_error 112

    to the source.  Or add

    --diag_error=112

    to the build options.  See slide 37 of the presentation on this page for more detail.

    Thanks and regards,

    -George

  • That's an interesting way to use runtime code for a compile time check. The problem I can see is that you should always get that warning regardless of the structure size. Just on different lines. In your example, line 3 should get the warning if the structure IS a multiple of 8 bytes. I am surprised that the Linux compiler would only complain about just one route through the code.