How to correctly assign a pointer returned by dlsym into a variable of function pointer type?
<p>I am trying to use <code>dlopen()</code> and <code>dlsym()</code> in my code and compile it with <code>gcc</code>.</p>

<p>Here is the first file.</p>

<pre><code>/* main.c */

#include &lt;dlfcn.h&gt;

int main()
{
    void *handle = dlopen("./foo.so", RTLD_NOW);

    if (handle) {
        void (*func)() = dlsym(handle, "func");
        func();
    }

    return 0;
}
</code></pre>

<p>Here is the second file.</p>

<pre><code>/* foo.c */

#include &lt;stdio.h&gt;

void func()
{
    printf("hello, world\n");
}
</code></pre>

<p>Here is how I compile and run the code.</p>

<pre><code>$ gcc -std=c99 -pedantic -Wall -Wextra -shared -fPIC -o foo.so foo.c
$ gcc -std=c99 -pedantic -Wall -Wextra -ldl -o main main.c
main.c: In function ‘main’:
main.c:10:26: warning: ISO C forbids initialization between function pointer and ‘void *’ [-Wpedantic]
         void (*func)() = dlsym(handle, "func");
                          ^
$ ./main
hello, world
</code></pre>

<p>How can I get rid of the warning?</p>

<p>Type casting doesn't help. If I try to type cast the return value of <code>dlsym()</code> into a function pointer, I get this warning instead.</p>

<pre><code>main.c:10:26: warning: ISO C forbids conversion of object pointer to function pointer type [-Wpedantic]
         void (*func)() = (void (*)()) dlsym(handle, "func");
                          ^
</code></pre>

<p>What would convince the compiler that this code is fine?</p>