Advantages of using extern after the declaration of fucntion pointer in the same header file

752 views Asked by At

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.

4

There are 4 answers

0
chqrlie On BEST ANSWER

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

extern initMyfuncs_t _initMyfuncs; // declaration of function pointer

myfuncs.c

initMyfuncs_t _initMyfuncs = some_function;  // actual definition of the pointer with optional initializer.

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.

0
alk On
extern int a;

is the declaraton of a. It is (just) a promise to the compiler a will be around at link-time.

It's actual definition needs to be done elsewhere, typically in a .c file via

int a = 42; /* The initialiser is optional, if missing it will default to '= 0'. */

The above concept is the same for any type.

0
Paul Ogilvie On

A header file can be included in many source files. Without the extern keyword, also storage would be alocated. Without the extern keyword in the header file, this storage would be allocated in every source file that includes the header file. At link time, the linker will now complain about multiple symbols (some linkers solve this gracefully).

To avoid this, yet not having to separately declare the objects in a source file, I use the following method:

/* myInclude.h */
#ifndef EXTERN
# define EXTERN extern
#endif
EXTERN int myGlobalVar;

/* someFile.c */
#include "myInclude.h"

/* main.c */
#define EXTERN
#include "myInclude.h"

This will allocate storage for the object in main.c and declare it as extern in all other souce files including the header file.

0
user2256825 On

i think i got the answer for that

1.without extern in header there would be a redefinition error , if we use the same variable (initMyfuncs_t _initMyfuncs = nullptr;)

  1. Or we can just mention typedef function pointer and initialize a varaible in source file

myfuncs.h

typedef void(*initMyfuncs_t)(int*, int*, int*);

and initiliaze it in source myfuncs.c

initMyfuncs_t _initMyfuncs = nullptr;     

and if we need declaration in header file then

myfuncs.h

typedef void(*initMyfuncs_t)(int, int, int);
extern  initMyfuncs_t _initMyfuncs;

and initialize it in source file

initMyfuncs_t _initMyfuncs = nullptr;

And finally when this header file is used in any source file, this variable can be used too as we have extern declared in it