I am actually trying to use a variable that is initialized in a header file(say x.h) and want to use same variable inside inlined code in the same header file. The same variable is modified in another file (say y.c). How can i do this ? I would like to know a good way of doing this.
Share
You can declare the global variable in the header file as
extern, and then define it inside a code-module (i.e., “.c” file). That way you won’t end up with multiple definition errors thrown by the linker.So for example in your header file, a globally available
intnamedmy_global_varwould have a declaration in a .h file that looks like:Then inside a single .c file somewhere you would define and initialize it:
Now you can use
my_global_varin any other code module that includes the appropriate header file and links with the proper .c file containing the definition of the global variable.