Extern variable

Discussion in 'C++' started by aki, Aug 29, 2008.

  1. aki

    aki Guest

    Hi i want to use a variable , who value should retain within function
    calls.

    File 1
    ..........

    static int flag=0

    func1()
    {
    func2();
    if (flag)

    {
    do somthing....
    }

    }



    File 2
    ...........

    extern int flag;

    func2()


    {

    flag=1;
    }



    Is this fine and work ????
     
    aki, Aug 29, 2008
    #1
    1. Advertising

  2. aki

    aki Guest

    On Aug 29, 10:19 am, aki <> wrote:
    > Hi i want to use a variable , who value should retain within function
    > calls.
    >
    > File 1
    > .........
    >
    > static int flag=0
    >
    > func1()
    > {
    > func2();
    > if (flag)
    >
    > {
    > do somthing....
    > }
    >
    > }
    >
    > File 2
    > ..........
    >
    > extern int flag;
    >
    > func2()
    >
    > {
    >
    > flag=1;
    >
    > }
    >
    > Is this fine and work ????


    Hiii All..


    one more thing i doubt.
    Should i include these declaratons and defination in .cc file or .h
    file


    Thanks
    aki
     
    aki, Aug 29, 2008
    #2
    1. Advertising

  3. aki

    Ian Collins Guest

    aki wrote:
    > Hi i want to use a variable , who value should retain within function
    > calls.
    >
    > File 1
    > ..........
    >
    > static int flag=0
    >

    <snip>
    >
    > File 2
    > ...........
    >
    > extern int flag;
    >

    <snip>
    >
    > Is this fine and work ????


    No, declaring flag 'static' will limit its scope to file 1, preventing
    it from being seen in file 2.

    --
    Ian Collins.
     
    Ian Collins, Aug 29, 2008
    #3
  4. aki

    Ian Collins Guest

    aki wrote:
    >
    > one more thing i doubt.
    > Should i include these declaratons and defination in .cc file or .h
    > file
    >

    Put the declaration in a header and the definition in one and only one
    source file.

    --
    Ian Collins.
     
    Ian Collins, Aug 29, 2008
    #4
  5. aki

    aki Guest

    On Aug 29, 10:31 am, Paavo Helde <> wrote:
    > aki <> kirjutas:
    >
    >
    >
    > > Hi i want to use a variable , who value should retain within function
    > > calls.

    >
    > > File 1
    > > .........

    >
    > > static int flag=0

    >
    > > func1()
    > > {
    > > func2();
    > > if (flag)

    >
    > > {
    > > do somthing....
    > > }

    >
    > > }

    >
    > > File 2
    > > ..........

    >
    > > extern int flag;

    >
    > > func2()

    >
    > > {

    >
    > > flag=1;
    > > }

    >
    > > Is this fine and work ????

    >
    > No, a static variable is seen only in one compilation unit. What you want
    > is:
    >
    > // in a header file included by both cpp-s:
    > extern int flag;
    >
    > // in one cpp file:
    > int flag = 0;
    >
    > Besides, why int and not bool?
    >
    > hth
    > Paavo




    hello All ,

    i tried like this ..but compilation is failing..;(

    one.h
    -------
    extern bool flag ;


    one.cc
    -------
    bool flag=0; // Should i place this inside fun1() ???

    func1()
    {
    func2();
    if (flag)

    {
    do somthing....
    }

    }


    two.cc
    ---------
    #include<one.h>

    fun2()
    {
    bool=1;
    }


    compilatuon error //
    Undefined first referenced
    symbol in file
    flag src/bssim/entitycom/
    EntitySblCom.o
    (here file name "EntitySblCom" refers to "two" )
     
    aki, Aug 29, 2008
    #5
  6. > Best advice is to not use global variables at all.
    > This advice is however difficult to follow, because even if you don't use
    > global variables directly, you can easily emulate the same via other means.

    Like having a billion pointers, spread out over a billion classes, all
    pointing to the same data, right?
     
    Anonymous Infidel - Aborted Islam with a hanger, Aug 29, 2008
    #6
  7. aki

    Rajib Guest

    aki wrote:
    > On Aug 29, 10:31 am, Paavo Helde <> wrote:
    >> aki <> kirjutas:
    >>
    >>
    >>
    >>> Hi i want to use a variable , who value should retain within function
    >>> calls.
    >>> File 1
    >>> .........
    >>> static int flag=0
    >>> func1()
    >>> {
    >>> func2();
    >>> if (flag)
    >>> {
    >>> do somthing....
    >>> }
    >>> }
    >>> File 2
    >>> ..........
    >>> extern int flag;
    >>> func2()
    >>> {
    >>> flag=1;
    >>> }
    >>> Is this fine and work ????

    >> No, a static variable is seen only in one compilation unit. What you want
    >> is:
    >>
    >> // in a header file included by both cpp-s:
    >> extern int flag;
    >>
    >> // in one cpp file:
    >> int flag = 0;
    >>
    >> Besides, why int and not bool?
    >>
    >> hth
    >> Paavo

    >
    >
    >
    > hello All ,
    >
    > i tried like this ..but compilation is failing..;(
    >
    > one.h
    > -------
    > extern bool flag ;
    >
    >
    > one.cc
    > -------
    > bool flag=0; // Should i place this inside fun1() ???
    >
    > func1()
    > {
    > func2();
    > if (flag)
    >
    > {
    > do somthing....
    > }
    >
    > }
    >
    >
    > two.cc
    > ---------
    > #include<one.h>
    >
    > fun2()
    > {
    > bool=1;


    Don't you mean flag=1;
    or flag=true;

    > }
    >
    >
    > compilatuon error //
    > Undefined first referenced
    > symbol in file
    > flag src/bssim/entitycom/
    > EntitySblCom.o
    > (here file name "EntitySblCom" refers to "two" )


    Looks like you're not linking the files.
    You need to compile the two files separately, and then link them together.

    On gcc the simple way to do it would look like:
    g++ file1.cpp file2.cpp
     
    Rajib, Aug 30, 2008
    #7
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,440
    tom_usenet
    Aug 2, 2004
  2. Dan Elliott
    Replies:
    4
    Views:
    13,266
    Tom Widmer
    Dec 8, 2004
  3. smackdab

    extern access to variable

    smackdab, Dec 8, 2003, in forum: C Programming
    Replies:
    1
    Views:
    450
    Jack Klein
    Dec 8, 2003
  4. Bangalore

    extern struct variable

    Bangalore, Aug 2, 2005, in forum: C++
    Replies:
    5
    Views:
    447
    Srini
    Sep 6, 2005
  5. Andre
    Replies:
    5
    Views:
    542
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page