CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2001
    Posts
    391

    #IFNDEF Preprocessor problem...HELP!

    I dont get it. I'm using MS VC++ 6 and have create 2 .CPP files and a single .H header file. Both .CPP files include the header file. The header file's contents are as follows:
    ---
    #ifndef __FOGHORN__
    #define __FOGHORN__

    int x;

    #endif
    ---

    On compiling i get the error message:
    ---
    file2.obj : error LNK2005: "int x" (?x@@3HA) already defined in file1.obj
    Debug/file1.exe : fatal error LNK1169: one or more multiply defined symbols found
    ---

    It seems the #IFNDEF preprocessor directive isnt functioning. Or maybe Im thinking it supposed to do something it's not. When I use the #IFNDEF/#DEFINE as I do in the header above isnt the preprocessor supposed to "remember" the new #DEFINE across the scope of the entire compiling process for all files in the project so that when the same #include file is included in another .CPP file the contents of the #IFNDEF/#ENDIF will NOT be included affectively avoiding an "already included" linking error? Or is it only effective during the compiling of a single .CPP file then when compiling begins for another the preprocessor clears its table starting fresh?

    Is this how it works??
    Last edited by quantass; December 23rd, 2002 at 08:49 PM.

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    No, the #ifndef is supposed to protect against

    #include <something.h>
    #include <morestuff.h>
    #include <something.h> // Duplicate include in SAME unit.


    You want to externally define the int in the header, then implement it in a single CPP.


    file: me.h
    extern int x;


    file: my.cpp
    #include "me.h"
    int x;
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

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