skaslev / gl3w

Simple OpenGL core profile loading
http://github.com/skaslev/gl3w
The Unlicense
709 stars 160 forks source link

Silence ISO C warning #34

Closed Leandros closed 7 years ago

Leandros commented 8 years ago

ISO C forbids casting between void* and any function pointer, since it assumes (for portabilities sake), they can have different widths. Therefore, compilers will spit out an ugly warning for code casting the result of dlsym to a function pointer.

The standard way of solving this is to replace the simple cast to function pointer, with a rather clumsy cast like this:

/* before: */
if (!res)
    res = (GL3WglProc) dlsym(libgl, proc);

/* after: */
if (!res)
    *(void **)(&res) = dlsym(libgl, proc);

Looks dangerous, but confirms to both ISO C and POSIX. Since POSIX.1-2013 every confirming implementation has to support casting between void* and function pointers. Nevertheless, most compilers still spit out a warning.

I've done this on my local fork, and can confirm it's working if compiled as C. No clue how a C++ compiler will react to this, though.

Leandros commented 7 years ago

👍