Extern Variable and/or #define in VC++

My application has about 30 classes.

I want to add a Global constant used by multiple programs called SWAP_ALLOWED which will be set to 1 or 2 based on queried value from a database.

I'd like this to be a #define variable OR extern.

I do know that #define cant be changed at runtime - OR can it be? If so, how?

But extern "C" int SWAP_ALLOWED is producing a LNK2001 error!

What do I do?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

#define cannot be changed at runtime because it is preproccesor definition, hence the pre prefix.

If you extern a variable, you can change that at runtime.  However by specifying extern you are saying that the variable is defined somewhere else.

so if you have in all your files where you need to use globalVar.

extern int globalVar;

you need to have

int globalVar;

in one file where the variable actually resides.

the LNK2001 is saying the linker can't find the,

int globalVar;

hope that helps.



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
How do I share data in my DLL with an application or with other DLLs?

if that variable is defined in a.exe and you want different instance of a.exe share that variable,
you can define it in a shared date segment like this(just as decribed in MSDN):
// shared data
#pragma data_seg(".SHARED")
int g_somedata=0;
#pragma data_seg()
#pragma comment(linker, "/section:.SHARED,RWS")

welcome to www.fruitfruit.com
uh, I didn't realize you were trying to use a variable from separate executables?  

If that is what you're trying to do, the response I posted will not work.  It only works from within the same executable.
AndyAinscowFreelance programmer / ConsultantCommented:
I want to add a Global constant used by multiple programs called SWAP_ALLOWED which will be set to 1 or 2 based on queried value from a database.

Why not just let each app query the database for the value?
Surya BobbadhiTechnical ConsultantCommented:
You can use Memory-Mapped files to share the memory between different executables.You can allocate a pagefile as shared memory in one app and access the same pagefile from other files.

From Master Application:Use below code
void CreateMemoryMappedFile()
         int*  pSharedVars;
         HANDLE hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,// Use swap file
                         NULL,// default security
                         PAGE_READWRITE,// read/write permission
                         0,// max. object size
                         1024,// size of hFile
                         "SampleMappingObject");// name of mapping object
         if (hMapFile == NULL)
      return ;
      pSharedVars = (int*)MapViewOfFile(hMapFile,// handle to mapping object
                   FILE_MAP_ALL_ACCESS,// read/write permission
                   0,// max. object size
                  0,// size of hFile
                             0);// map entire file
      if (pSharedVars == NULL)
                        pSharedVars[0] = 12;
                        pSharedVars[1] = 13;


From Other Applcations follow below code:
void CreateMemoryMappedFile()
         int*  pSharedVars = NULL;
         hFMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "SampleMappingObject");

        if (hFMapFile != NULL)
            // Obtaining the base address of the mapping
            lpFMapAddress = MapViewOfFile(hFMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
           if (lpFMapAddress != NULL)
        pFSharedVars = (SHAREDVARS*)lpFMapAddress;

I am already using the same in my programs and it is working fine.With this your problem wiil be solved.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.