Dynamic Shared Library compilation with g++

前端 未结 4 1423
别跟我提以往
别跟我提以往 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:11

    dlsym returns a pointer to the symbol. (As void* to be generic.) In your case you should cast it to a function-pointer.

     double (*mycosine)(double); // declare function pointer
     mycosine = (double (*)(double)) dlsym(handle, "cos"); // cast to function pointer and assign
    
     double one = mycosine(0.0); // cos(0)
    

    So this one of these rare cases where the compiler error is a good clue. ;)

提交回复
热议问题