In the code below, can it be expained why extern has been used right after the declaration on function pointer.
myfuncs.h
typedef void (*initMyfuncs_t)(Init_t*, CallBacks_t *,result_t*);
extern initMyfuncs_t _initMyfuncs;
and we are using it in myfunc.c file
void *glbfuncs=NULL
glbfuncs = dlopen("glbfuncs.so",RTLD_NOW);
initMyfuncs_t _initMyfuncs=NULL;
_initMyfuncs = dlsym(glbfuncs, "_initMyfuncs");
Usually,we use extern in the files or other header file where we use this function pointer or variable. Here we have declared extern in header file and using it in same source file. what is the use of using extern here
And this function pointer is being used in other source files without extern declaration . iam confused with this? Generally, we have a declaration somewhere (somehere.h) and if we use it in some other file(here.h or here.c) we use extern keyword just to get this variable.
I did not quite understand the reason for using extern keyword after typedef.
Why do we use extern after the declaration of fucntion pointer in the same header file.
The
extern
keyword is needed to distinguish the declaration of a global data object such as a function pointer and the definition of this object.In a header file, you should only have declarations. Definitions belong in source files and should not be duplicated in different source files. If you put definitions in header files, you end up with multiple definitions of the same symbols at link time. For historical reasons, common linkers accept these multiple definitions if they do not have initializers. Nevertheless, it is considered bad practice and should be avoided.
myfuncs.h
myfuncs.c
The confusion comes from the typedef of a function pointer, different from a function prototype. A function prototype is a declaration even without an
extern
keyword.