CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2002
    Posts
    41

    Unhappy How to define a global variable in .h file?

    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.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    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]

  3. #3
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    166
    in addition to TheCPUWizard's post - the links to these threads are:

    1: how to have varibles globle to a file?
    2: how to have varibles globle to a file?

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

    Mikey

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured