Hello,
Compiling using gcc
I am working on a project that has some modules that will share a common handler. So I am using a extern
I have created a cut down version of my project.
However, I get an undefined reference hLibrary in library.c.
Is this the correct way to do this using extern. Or is there a better way to share a global that is safer?
Many thanks,
library.h
library.cCode:#ifndef LIBRARY_H_INCLUDED #define LIBRARY_H_INCLUDED int open_library(); #endif // LIBRARY_H_INCLUDED
adapter.hCode:#include <stdio.h> #include "library.h" extern int hLibrary; int open_library() { printf("The library has been opened"); //Set the value for this handler so it can be //used in another module (adapter.c) hLibrary = 1010; return 1; }
The module implementation file that will use the hLibrary handler.Code:#ifndef ADAPTER_H_INCLUDED #define ADAPTER_H_INCLUDED int find_adapter(); #endif // ADAPTER_H_INCLUDED
Code:#include <stdio.h> #include "adapter.h" #include "library.h" int find_adapter() { printf("\nStarting adapter"); int hLibrary; if(hLibrary == 1010) { printf("\nAdapter has been started: %d", hLibrary); return 1; } else { printf("\nCould not find hLibrary"); } return 1; }



LinkBack URL
About LinkBacks


