嗨,我想實現共享庫(動態鏈接)下面是我收到的錯誤爲下面的代碼,請幫我解決它 將共享庫作爲「無效轉換」從void *轉換爲double(*)(int *)時出錯?
error: invalid conversion from ‘void*’ to ‘double (*)(int*)’ [-fpermissive]
fn = dlsym(lib_handle, "ctest1");
ctest1.c
void ctest1(int *i)
{
*i=5;
}
以上ctest1.c是在以下hello.cc文件中使用的共享庫
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include "ctest1.h" // here i have declared the function of shared library
int main(int argc, char **argv)
{
void *lib_handle;
void (*fn)(int *);
int x=990;
char *error;
lib_handle = dlopen("libp.so", RTLD_LAZY); // opening the shared library
if (!lib_handle)
{
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
fn = dlsym(lib_handle, "ctest1"); //storing the address of shared library function
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "%s\n", error);
exit(1);
}
fn(&x);
printf("getting x value from shared library=%d\n",x);
dlclose(lib_handle);
return 0;
}
〜
〜
〜
這看起來不像C++代碼。不要將C或C++標籤添加到無關的問題中。 – Olaf