09-19-2011 06:28 PM
Hi,
I have written two libraries, both libraries need to share access to a variable, GblVal. So in one lib, when GblVal is changed, the other lib should also see this change.
I have tried to achieve this through extern in the following setup:
lib1:
lib1.c:
int GblSum;
...
void Do(){
GblSum=1;
}
lib1.h:
extern int GblSum;
lib2.c:
#include "lib1.h"
void check(){
printf("%d",GblSum);
}
finally I have a main process that has both compiled libs
main.c:
Do(); check();
obviously my code doesn't actually do the above, but this is just the gist of what I'm trying to do. I'm fairly certain that I am setting my extern variable up right, however I can't compile lib2 due to:
Undefined symbol "_GblNum" referenced in lib2.c
what am I doing wrong? I have suspicions that this is a linker error but I'm not really sure how to fix it.
thanks
09-20-2011 12:39 PM
First, I'm not sure if you copy and pasted the linker error or if you just mistyped, but the error reports GlbNum instead of GlbSum. I'm assuming this was a typo in the post.
Second, if you define the variable in lib1.c, then you will want to place "extern GlbSum;" in lib2.h (or lib2.c) instead of lib1.h.
09-20-2011 12:50 PM - edited 09-20-2011 12:52 PM
yea it's a typo. But is there a need for extern GlbSum to be in lib2.h? I included lib1.h to lib2.c, which also has that declaration..
do you mean to instead include lib2.h in lib1.c?
09-20-2011 04:28 PM
This is getting a bit confusing. Let's just describe how it is supposed to work instead.
A variable needs to be both defined and declared. It can be declared in many places and multiple times within the same scope, but may only be defined once.Anywhere the variable is used, it will need to have been declared within the scope.
Therefore, typically you will declare the variable once in say lib1.c:
#include lib1.h int myGlobal = 1;
and then anywhere that you want to use it, make sure it is defined in the scope. Typically you will do this in the header associated with the file for instance given lib2.c and lib2.h:
//lib2.h extern myGlobal; int Foo();
//lib2.c
#include "lib2.h"
int Foo()
{
return myGlobal;
}
But so long as the variable is declared within the include list, you should be covered. You could place it in lib1.h instead and include that in lib2.h or lib2.c.
09-20-2011 04:57 PM
I think I was a bit unclear about the relationship between the two libs. They are not under the same workspace or project and are thus separate from one another completely. I have tried compiling one into a library and adding to the other with no success.
I did as your example suggested and have still come across the same undefined symbol error as before.
09-21-2011 07:30 AM
Are you sure both projects are set to create static libraries? There is no linking phase associated with the creation of static libraries and CVI certainly doesn't complain in my machine if I create the equivalent of your 'lib2'.
09-21-2011 12:26 PM
wow thanks! I was creating dynamic libs, thats what was giving me so much of a headache. What is the difference of a static lib compared to a dynamic lib?
09-22-2011 07:51 AM
Static libraries (usual file extension .lib) are simply collections of compiled object files. To use functions and data contained in a static library in an application, the library must be specified as one of the input files to the application. At link time, the linker searches the library to resolve unsatisfied function and data references and extracts the object modules required to satisfy them.
Dynamic Link Libraries (usual file extension .dll) are compiled and linked code modules (that is, they contain their own run-time support) that expose the addresses of particular functions (and, less often, data items) contained within the module. If an application uses functions from a dynamic link library, the DLL is also loaded when the application is run and the function/data addresses made known to the application. DLLs are most often used to share common code between applications, but because they are self-contained they are also a common way of building applications using different technologies (e.g. a CVI application can use a DLL built using Delphi or VC++).
Normally, a DLL is associated with an import library which is linked in with the calling application. This import library resolves references to functions and data in the DLL used by the calling application by 'telling' the application which DLL the functions are in and how to find them in that DLL. Applications can also explicitly load DLLs at run-time if required.