Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files

Discussion in 'C++' started by Christopher M. Lusardi, Sep 2, 2004.

  1. Hello,

    How is this possible. If I have two separate files that include the
    same dot h file as described below why am I allowed to access the same
    value for a constant. I compiled this program using a makefile. (Sorry,
    I can't post the long makefile or program.)

    What the code is suppose to do is: main () includes the file and the
    variable multible_defined_var actually gets defined. file2.c will get the
    external definition only because now the constant TESTC is defined.

    If I fool around with the makefile I can remove the error saying variable
    multible_defined_var is multiply defined and the program works fine.

    The reason I have to know is if I create another test program and define
    just a constant without any other pre-compiler directives involved in just
    main() it's undefined in other program files.

    file1.c
    -------

    #include "test.h"

    main ()
    {
    ....

    t2 ();
    }


    test.h
    ------
    #ifndef TESTC
    #define TESTC
    int multible_defined_var;
    #else
    extern int multible_defined_var;
    #endif


    file2.c
    -------
    #include "test.h"


    void t2 ()
    {
    ....
    }


    Thank you,
    Christopher Lusardi

    P.S.: I apologize if there is a typo in the code I posted.
     
    Christopher M. Lusardi, Sep 2, 2004
    #1
    1. Advertising

  2. Christopher M. Lusardi

    Ian Guest

    Re: Understanding How To Use #ifdef Constant #define Constant SequenceIn Multible Files

    Christopher M. Lusardi wrote:
    > Hello,
    >
    > How is this possible. If I have two separate files that include the
    > same dot h file as described below why am I allowed to access the same
    > value for a constant. I compiled this program using a makefile. (Sorry,
    > I can't post the long makefile or program.)
    >
    > What the code is suppose to do is: main () includes the file and the
    > variable multible_defined_var actually gets defined. file2.c will get the
    > external definition only because now the constant TESTC is defined.
    >
    > If I fool around with the makefile I can remove the error saying variable
    > multible_defined_var is multiply defined and the program works fine.
    >
    > The reason I have to know is if I create another test program and define
    > just a constant without any other pre-compiler directives involved in just
    > main() it's undefined in other program files.
    >

    put the line

    int multible_defined_var;

    in one of the c files, not the header!

    Ian

    > file1.c
    > -------
    >
    > #include "test.h"
    >
    > main ()
    > {
    > ...
    >
    > t2 ();
    > }
    >
    >
    > test.h
    > ------
    > #ifndef TESTC
    > #define TESTC
    > int multible_defined_var;
    > #else
    > extern int multible_defined_var;
    > #endif
    >
    >
    > file2.c
    > -------
    > #include "test.h"
    >
    >
    > void t2 ()
    > {
    > ...
    > }
    >
    >
    > Thank you,
    > Christopher Lusardi
    >
    > P.S.: I apologize if there is a typo in the code I posted.
     
    Ian, Sep 2, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SSBhbSBTYW0=?=

    DataRelation on Multible layers of DataRelations

    =?Utf-8?B?SSBhbSBTYW0=?=, Mar 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    537
    Karl Seguin
    Mar 6, 2005
  2. Softari
    Replies:
    1
    Views:
    430
    John Harrison
    Jul 14, 2004
  3. Softari
    Replies:
    1
    Views:
    380
    red floyd
    Jul 14, 2004
  4. Replies:
    4
    Views:
    1,292
    CBFalconer
    Jul 7, 2005
  5. #ifdef within a #define

    , Feb 17, 2007, in forum: C Programming
    Replies:
    6
    Views:
    447
    SM Ryan
    Feb 19, 2007
Loading...

Share This Page