Silence WCast-function-type on a function structure

wno-cast-function-type
gcc cast-function-type
unrecognized command line option ‘-wno-cast-function-type’
g++ disable warning
wno-error=all
gcc suppress warning
gcc warnings as errors
gcc suppress warning pragma

I have the following warning :

cast between incompatible function types from ‘int (*)(pile *)’ {aka ‘int (*)(struct pile *)’} to ‘void (*)(void *)’ [-Wcast-function-type] 

I don't have any idea of how I can fix this warning.

Structure

typedef struct conteneur_sommets {
  void *donnees;
  int (*est_vide)(void *);
  void (*ajouter)(void *, int);
  void (*supprimer)(void *);
  int (*choisir)(void *);
  void (*detruire)(void *);
} conteneur_sommets;

associated function

conteneur_sommets *cs_creer_pile(int n)
{
  conteneur_sommets cs = {.donnees   = pile_creer(n),
              .est_vide  = (int (*)(void *))        pile_est_vide,
              .ajouter   = (void (*)(void *, int))  pile_empiler,
              .supprimer = (void (*)(void *))       pile_depiler, <- warning cast
              .choisir   = (int (*)(void *))        pile_sommet,
              .detruire  = (void (*)(void *))       pile_detruire};
  return cs_creer(&cs);
}

void cs_supprimer(conteneur_sommets *cs)
{
  cs->supprimer(cs->donnees);
}

While suppressing the warning completely can sometimes be usefull, it is better in general to fix the issue at the code level. The issue here is that your declarations are wrong which is reason the warning is triggered in the first place. First avoid to combine the definition of a struct/union and the declaration of the typedef alias. It is better to separate them, this way you can, if necessary separate declaration and definition.

typedef struct conteneur_sommets conteneur_sommets;

This creates the alias conteneur_sommets to the opaque structure struct conteneur_sommets. This is also called a forward declaration. The forward declaration is generally put in the header file.

Now we can define the structure

struct conteneur_sommets {
    void *donnees;
    int (*est_vide)(conteneur_sommets *);
    void (*ajouter)(conteneur_sommets *, int);
    void (*supprimer)(conteneur_sommets *);
    int (*choisir)(conteneur_sommets *);
    void (*detruire)(conteneur_sommets *);
};

This way you don't need to cast the function pointer and conserve type checks if it is of the right signature.

conteneur_sommets cs = {.donnees   = pile_creer(n),
          .supprimer = pile_depiler};

Secondly A thing that is seldom realized by C programmers is that one can define typedef aliases of function prototypes, not function pointers but directly of prototypes. This can sometime really make the code much more readable even if quite unusual. Let me give you an example with your code:

typedef int  est_vide_fn (conteneur_sommets *);
typedef void ajouter_fn  (conteneur_sommets *, int);
typedef void supprimer_fn(conteneur_sommets *);
typedef int  choisir_fn  (conteneur_sommets *);
typedef void detruire_fn (conteneur_sommets *);

this declares five function prototype type definitions.

the structure can now be declared as

struct conteneur_sommets {
    void *donnees;
    est_vide_fn  *est_vide;
    ajouter_fn   *ajouter;
    supprimer_fn *supprimer;
    choisir_fn   *choisir;
    detruire_fn  *detruire;
};

Your cs_supprimer functions prototype can be declared by

supprimer_fn cs_supprimer;

and its implementation is defined as before

void cs_supprimer(conteneur_sommets *cs)
{
    cs->supprimer(cs->donnees);
}

build: Turn off -Wcast-function-type (#96) � Issues � GNOME / gnome , GCC 8 introduced `-Wcast-function-type`. And that is GCallback, so you can silence the warning by casting through GCallback, which is what� Curl is a tool and libcurl is a library for transferring data with URL syntax, supporting FTP, FTPS, HTTP, HTTPS, GOPHER, TFTP, SCP, SFTP, TELNET, DICT, LDAP, LDAPS, FILE, IMAP, SMTP, POP3, RTSP and RTMP. libcurl offers a myriad of powerful features - MarcelRaad/curl

cast between incompatible function types from ‘int (*)(pile *)’ {aka ‘int (*)(struct pile *)’} to ‘void (*)(void *)’ [-Wcast-function-type]

This warning means two function pointer's prototype is differ..

supprimer and pile_depiler has different prototype ( return value type and parameter type )

match two function's prototype then the warning will be disappear. Simply you can do cast prototype but the result is not predictable.

Many -Werror=cast-function-type with GCC 8 � Issue #2 , In order to silence the warning, I can think of two possible ways: either by adding code in setup.py which tries running $CC -Wcast-function-type� bugfix: silenced -Wcast-function-type warnings #1374 ghedo wants to merge 1 commit into openresty : master from unknown repository Conversation 2 Commits 1 Checks 0 Files changed

I have silenced it by having a pragma macro

#pragma GCC diagnostic ignored "-Wcast-function-type"

// some affected code

#pragma GCC diagnostic pop

Is is possible to work around -Wcast-function-type warnings , Is is possible to work around -Wcast-function-type warnings produced Though, aside from silencing the errors there seems to be no solution. In order to make sure new function cast mismatches are not introduced in the kernel (to avoid tripping CFI checking), the kernel should be globally built with -Wcast-function-type. Many call sites need to be fixed before this can happen.

Warning Options (Using the GNU Compiler Collection (GCC)), -Wclobbered -Wcast-function-type -Wdeprecated-copy (C++ only) Instead of these attributes, it is also possible to add a fallthrough comment to silence the warning. Warn when the indentation of the code does not reflect the block structure. -Wcast-function-type. Warn when a function pointer is cast to an incompatible function pointer. In a cast involving function types with a variable argument list only the types of initial arguments that are provided are considered. Any parameter of pointer-type matches any other pointer-type.

Issue 33012: Invalid function cast warnings with gcc 8 for , The most common is the cast of a METH_NOARGS function (that uses just one function types from 'PyObject * (*)(striterobject *)' {aka 'struct _object as done in those PRs to silence the warning when cast to PyCFunction. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

#1865 (error: cast between incompatible function types from , error: cast between incompatible function types from 'FARPROC' on MinGW ULONG, INT)' {aka 'int (*)(struct pollfd *, long unsigned int, int)'} [-Werror=cast- function-type] Win32: silenced -Wcast-function-type GCC warning (ticket #1865 ). We are going to suppress these warnings for now with -Wno-cast-function-type , but should stop using this flag in the future when these warnings are handled by V8. This comment has been minimized. Sign in to view

Comments
  • Try (void (*)()) to not provide signature info.