Click to See Complete Forum and Search --> : Extern variable problem
aldarn
September 18th, 2002, 03:30 AM
Can anyone tell me if I can do the following:
Define a global variable in one cpp/c file (call it file1.c)
Then have it as an extern in a second file (call it file2.c),
but don't allow it to be changed from this file.
for example:
/****file1.c********/
double NiceVar;
void function()
{
NiceVar = 100; // I can set the value in file1.c easily.
}
/****************/
/****file2.c********/
extern double NiceVar;
void main()
{
NiceVar = 1000; // This shouldn't work.
printf( "%f", NiceVar ) // But this should return 100
}
/****************/
You'll have to excuse the basic explanation, but I am still a beginner!
Graham
September 18th, 2002, 03:59 AM
// file 1.cpp
double niceVar;
// file1.h (or file2.cpp)
extern const double niceVar;
aldarn
September 19th, 2002, 03:32 AM
Thanks for your help Graham. I've tried this but I am still
having problems, however I think it may be a compiler related issue. I've compiled this code using MS Visual C++ and it gives me the following error:
ile2.obj : error LNK2001: unresolved external symbol "double const NiceVar" (?NiceVar@@3NB)
Debug/Code1.exe : fatal error LNK1120: 1 unresolved externals
I've also compiled it using a Unix version of GCC and it seems to link fine.
Any ideas?
stober
September 19th, 2002, 06:06 AM
Are you sure both files have a *.c extension? The compiler does not mangle the names of objects in a *.c file, but it does in *.cpp files. In a *.cpp file you should declare it like this:
extern "C" double NiceVar;
aldarn
September 19th, 2002, 08:27 AM
I am actually using cpp files stober, but the fix you mentioned
seems to give a different error. The code is thus:
/****file1.cpp********/
#include "stdio.h"
double NiceVar;
void function()
{
NiceVar = 100; // I can set the value in file1.cpp easily.
}
/****************/
/****file2.cpp********/
#include "stdio.h"
extern "C" const double NiceVar;
void main()
{
//NiceVar = 1000; // This shouldn't work.
printf( "%f", NiceVar ); // But this should return 100
}
/****************/
When I compile in Vis C++ I get:
Linking...
file2.obj : error LNK2001: unresolved external symbol _NiceVar
Debug/Code1.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Sorry to keep going on about this, but its one if those annoying things where you can't go any further on a project until you've got it sorted!
AnthonyMai
September 19th, 2002, 09:45 AM
Obviously the answer given by Graham would not work. A value declared as const or none-const have different storage requirements. Const values are either resolved inline or stored in a read-only section of memory. None const values are stored in a writable memory section. So if you declare it as const in one file and none const in another file, it will not be resolved.
If the global value you declared is changeable in one file, there is really nothing that can prevent another file from changing it. But there may be ways to aviod the second file from changing it accidentally or unintentionally.
Declare the global valuable as static in the file where it is defined. That way other files would not be able to see it, althrough they can still change or access it had they know the address. Next, declare a global const pointer which points to the address of your global variable. Expose this const pointer to other files. This way other files can access the variable through this const pointer. But they can not change the value since it is a const pointer.
Those files that wants to change the value of the global variable can do so by explicitly casting the const pointer to a none-const pointer, and then they can use the none-const pointer to modify the global variable.
Certainly if any file want to modify the global variable, they can do the const-to-none-const casting too and gain access. The point is hopefully the extra steps would prevent accidental and un-intentional modification. As for intensional access, nothing can be done to prevent that.
cup
September 19th, 2002, 10:37 AM
You can also use references as a variant on Anthony's suggestion.
// file 1.cpp
double horribleVar;
const double& niceVar = horribleVar;
// file1.h (or file2.cpp)
extern const double& niceVar;
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.