Search code examples
clinuxgccx86-64glibc

Called object is not a function or a function pointer


I have the following code.

typedef pid_t (*getpidType)(void);

pid_t getpid(void)
{
    printf("Hello, getpid!\n");
    getpidType* f = (getpidType*)dlsym(RTLD_NEXT, "getpid");
    return f(); // <-- Problem here
}

The compiler complains that called object ‘f’ is not a function. What is going on here? Haven't I declared and used the function pointer f in a correct way?


Solution

  • getpidType is already a pointer, so drop the *:

    getpidType f = (getpidType)dlsym(RTLD_NEXT, "getpid");
    

    (Even better, drop the explicit cast as well:

    getpidType f = dlsym(RTLD_NEXT, "getpid");
    

    Since dlsym returns void* and void* is implicitly convertible to any other pointer type, the cast is not needed. It may even hide bugs.)