Dynamic Shared Library compilation with g++

前端 未结 4 1424
别跟我提以往
别跟我提以往 2021-01-30 18:44

I\'m trying to compile the following simple DL library example code from Program-Library-HOWTO with g++. This is just an example so I can learn how to use and write shared libr

4条回答
  •  梦如初夏
    2021-01-30 19:09

    C allows implicit casts from void * to any pointer type (including function pointers); C++ requires explicit casting. As leiflundgren says, you need to cast the return value of dlsym() to the function pointer type you need.

    Many people find C's function pointer syntax awkward. One common pattern is to typedef the function pointer:

    typedef double (*cosine_func_ptr)(double);
    

    You can define your function pointer variable cosine as a member of your type:

    cosine_func_ptr cosine;
    

    And cast using the type instead of the awkward function pointer syntax:

    cosine = (cosine_func_ptr)dlsym(handle, "cos");
    

提交回复
热议问题