Is strongly-typed programming conceivable for C code using gcc ?

Alexandre Beaugy beaugy.ext@tlse.sofreavia.fr
Wed Sep 19 14:50:00 GMT 2007


Hi all,

Better a good example than a sermon to illustrate my question.

The project background is the following: I’m working on a C++ Library wrapper in C. So for each class wrapped (represented in the following example by "C_MsgUp" and "C_MsgDown"), I create an equivalent C type (e.g: respectively "CW_C_MsgUp_t" and "CW_C_MsgDown_t"). It is in the use of my defined C types that I encounter a problem.

Here is the problem encountered in my C source code compiled with `gcc':
(To make it easier to understand, consider "CW_C_MsgUp_t" and "CW_C_MsgDown_t" C types, wrapping respectively "C_MsgUp" and "C_MsgDown" C++ classes, as completely different and incompatible, even if they are similar in reality. Therefore, not "cast-able").

<c_source_code>
  typedef  void  CW_C_MsgUp_t; 
  typedef  void  CW_C_MsgDown_t;

  int sendMsg(CW_C_MsgUp_t *up_msg);
  int getMsg(CW_C_MsgDown_t *down_msg);

  int main(int argc, char **argv)
  {
    CW_C_MsgUp_t *my_up_msg; 
    CW_C_MsgDown_t *my_down_msg;

    my_up_msg = CW_C_MsgUp_new(); /* C_MsgUp constructor wrapper call */
    my_down_msg = CW_C_MsgDown_new(); /* C_MsgDown constructor wrapper call */

    sendMsg(my_up_msg); /* OK */

    getMsg(my_up_msg); /* KO: accepted by gcc (at compilation time),
                        *     where it ought to be refused (as I
                        *     expected it to)! */

    getMsg(my_down_msg); /* OK: expected good use of the function */

    CW_C_MsgUp_delete (my_up_msg); /* C_MsgUp destructor wrapper call */
    CW_C_MsgDown_delete(my_down_msg); /* C_MsgDown destructor wrapper call */

    return 0;
  }
</c_source_code>

So as explained in the source code comments, my problem is the following: is it possible to ask gcc to warn me each time a call to a function is incompatible with the function prototype and the given parameters' type ?

I tried "-Wall", "-Wextra", "-Wstrict-prototypes", ..., with no success at all!

Can anyone please help me?

Thanx a lot for your assistance.


Regards.


-- 
BEAUGY Alexandre
Real-time/UNIX Software Engineer
http://www.eurogiciel.fr/
tel. : +33.(0)5.61.00.79.79
fax. : +33.(0)5.61.39.01.15




More information about the Gcc-help mailing list