Click to See Complete Forum and Search --> : How to define a global variable in .h file?


pardxa
December 8th, 2002, 07:21 AM
I am trying to keep all global variables in one .h file.here is a example like this:

// stdafx.h

#if !defined(AFX_STDAFX)
#define AFX_STDAFX

#include <stdio.h>
char word[20];

#endif

//main.cpp
#include "stdafx.h"
int main(int argc, char* argv[])
{
strcpy(word,"ok ,you r welcome",20);
printf(word);
return 0;
}

but the result like this:

Linking...
StdAfx.obj : error LNK2005: "char * word" (?word@@3PADA) already defined in console.obj
Debug/console.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.

what should i do, if i intend to keep all global variables in a header file.

TheCPUWizard
December 8th, 2002, 08:07 AM
Please see other posts on globals and headers, there is good information already on the board. Specifically:

"how to have varibles globle to a file". [spellingsmatch actual subject line]

Mikey
December 8th, 2002, 11:01 AM
in addition to TheCPUWizard's post - the links to these threads are:

1: how to have varibles globle to a file? (http://www.codeguru.com/forum/showthread.php?s=&threadid=221762&highlight=varibles+globle)
2: how to have varibles globle to a file? (http://www.codeguru.com/forum/showthread.php?s=&threadid=221763&highlight=varibles+globle)

(it's easier to click, than to search :) )

Mikey