I am trying to compile the following simple DL library example code from G ++. This is just an example, so I can learn to use and write a shared library. The actual code for the developed library I will be written in C ++
#include & lt; Stdlib.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Dlfcn.h & gt; Int main (int argc, char ** argv) {zero * handle; Double (* cosine) (double); Four * error; Handle = DLPN ("/lib/libm.so.6", RTLD_LAZY); If (handle!) {Fputs (deller (), stderr); Exit (1); } Cosine = dlsym (handle, "cos"); If ((error = dlerror ())! = NULL {fputs (error, stderr); Exit (1); } Printf ("% f \ n", (* cosine) (2.0)); Dlclose (handle); }
If I compile the program with GCC then it works fine.
gcc -o foo.c -ldl
When I change the file name and compiler to the following
G ++ -o foo foo.cpp -ldl
I get the following error: (I)>
foo.cpp: 16: error : 'Zero *' to 'Double (*)' Invalid Conversion from ''
I think if this is wrong then correct me) that I have zero in C ++ An underlying artist can not do this from the pointer, but C gives me, and that is why the above code is used by GCC Do not use G ++, so I tried to make a clear artist by changing the 16 line above:
cosine = (double *) dlsym (handle, "cos");
Instead, I get the following error:
foo.cpp: 16: Error: 'double *' to 'double' *] ( Double in 'Assignment'
In these problems, maybe some more appropriate C ++ coding standards have to do with their own general ignorance. Do I need to develop dynamic libraries for Linux Can you tell a good tutorial for which uses the C ++ example code?
C from any indicator type (including function pointer) to void *
The underlying derivative allows for; C ++ requires explicit castings, as Lifolgangen says, you need to enter the return value of the function pointer type of dlsym ()
.
Many people find the function pointer syntax weird to find a common pattern that is typical of the function pointer:
typingf double (* cosine_func_ptr) (double);
You can define your function pointer variable cosine
as your type of member:
cosine_func_ptr cosine;
Use the type instead of the strange function pointer syntax:
cosine = (cosine_func_ptr) dlsym (handle, "cos");
Comments
Post a Comment