Portál AbcLinuxu, 16. července 2025 21:06
void *lib=0; lib=dlopen("./libeng.so.1.0.0", RTLD_LAZY); int (*hello)() = (int(*)()) dlsym(lib, "hello"); hello(); dlclose(lib);prekladam to: gcc -ldl -rdynamic -std=c99 -pedantic -Wall -o test ./main.c a dostanem warning: ISO C forbids conversion of object pointer to function pointer type. Neviete prosim, ci mam zle to pretypovanie alebo to proste ISO C zakazuje?
typedef int (*Foo)(int); int f(int x) { return x+1; } int main(void) { void *p = &f; Foo g = p; return 0; }je normálně přeložitelný (a funguje, pokud se g následně k něčemu použije).
man dlsym
/* Writing: cosine = (double (*)(double)) dlsym(handle, "cos");
would seem more natural, but the C99 standard leaves
casting from "void *" to a function pointer undefined.
The assignment used below is the POSIX.1-2003 (Technical
Corrigendum 1) workaround; see the Rationale for the
POSIX specification of dlsym(). */
*(void **) (&cosine) = dlsym(handle, "cos");
Takze:
int main(){
void *lib = NULL;
int (*hello)();
lib = dlopen("./libeng.so.1.0.0", RTLD_LAZY);
*(void**) (&hello) = dlsym(lib, "hello");
hello();
dlclose(lib);
return 0;
}
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.