static extern?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gernot Frisch

    static extern?

    Problem:

    A.cpp:
    ------
    static FOO* gFoo=NULL;

    A.h
    extern FOO* gFoo;

    gives: L2001 - unresolved external: "symbol struct FOO* gFoo"

    if A.h looks like:
    extern static FOO* gFoo;

    it gives:
    C2159: more than one storage class specified.

    <blink, blink>
    How would I do this now? I have a prefedined macro that makes a
    "static FOO*". And I want to tell all my other .cpp files that it
    exists in stdafx.h.

    Thank you,


    --
    -Gernot
    int main(int argc, char** argv) {printf
    ("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}

    _______________ _______________ __________
    Looking for a good game? Do it yourself!
    GLBasic - you can do
    GLBasic is a programming language that supports multiple platforms like e.g. iPhone



  • Vince Yuan

    #2
    Re: static extern?

    Try this:

    A.h
    static FOO* gFoo;

    A.cpp:
    extern static FOO* gFoo=NULL;

    Vince

    "Gernot Frisch" <Me@Privacy.net > wrote in message
    news:2ka664F19s 8g2U1@uni-berlin.de...[color=blue]
    > Problem:
    >
    > A.cpp:
    > ------
    > static FOO* gFoo=NULL;
    >
    > A.h
    > extern FOO* gFoo;
    >
    > gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
    >
    > if A.h looks like:
    > extern static FOO* gFoo;
    >
    > it gives:
    > C2159: more than one storage class specified.
    >
    > <blink, blink>
    > How would I do this now? I have a prefedined macro that makes a
    > "static FOO*". And I want to tell all my other .cpp files that it
    > exists in stdafx.h.
    >
    > Thank you,
    >
    >
    > --
    > -Gernot
    > int main(int argc, char** argv) {printf
    > ("%silto%c%cf%c gl%ssic%ccom%c" , "ma", 58, 'g', 64, "ba", 46, 10);}
    >
    > _______________ _______________ __________
    > Looking for a good game? Do it yourself!
    > GLBasic - you can do
    > www.GLBasic.com
    >
    >[/color]


    Comment

    • Mark Warren

      #3
      Re: static extern?

      Gernot Frisch wrote:[color=blue]
      > Problem:
      >
      > A.cpp:
      > ------
      > static FOO* gFoo=NULL;
      >
      > A.h
      > extern FOO* gFoo;
      >
      > gives: L2001 - unresolved external: "symbol struct FOO* gFoo"
      >
      > if A.h looks like:
      > extern static FOO* gFoo;
      >
      > it gives:
      > C2159: more than one storage class specified.[/color]

      Yes, the variable gFoo cannot be static and extern at the same time.
      [color=blue]
      > <blink, blink>
      > How would I do this now? I have a prefedined macro that makes a
      > "static FOO*". And I want to tell all my other .cpp files that it
      > exists in stdafx.h.
      >
      > Thank you,[/color]

      Solution:

      A.cpp:
      ------
      FOO* gFoo=NULL; // Note no static

      A.h
      ----
      extern FOO* gFoo;


      HTH

      Mark


      Comment

      • Gernot Frisch

        #4
        Re: static extern?

        > Solution:[color=blue]
        >
        > A.cpp:
        > ------
        > FOO* gFoo=NULL; // Note no static
        >
        > A.h
        > ----
        > extern FOO* gFoo;[/color]

        I rewrote the code so I can do this now. But I don't understand it.
        Why can't I tell another .cpp file that there is an static variable
        somewhere else?
        -Gernot


        Comment

        • Sharad Kala

          #5
          Re: static extern?


          "Gernot Frisch" <Me@Privacy.net > wrote in message
          news:2kaegtF19v 3h6U1@uni-berlin.de...[color=blue][color=green]
          > > Solution:
          > >[/color]
          >
          > I rewrote the code so I can do this now. But I don't understand it.
          > Why can't I tell another .cpp file that there is an static variable
          > somewhere else?[/color]

          Because static variables having namespace scope have internal linkage i.e.
          the entity it denotes can be referred to by names from other scopes in the
          same translation unit only.


          Comment

          • Rolf Magnus

            #6
            Re: static extern?

            Gernot Frisch wrote:
            [color=blue]
            > Problem:
            >
            > A.cpp:
            > ------
            > static FOO* gFoo=NULL;
            >
            > A.h
            > extern FOO* gFoo;
            >
            > gives: L2001 - unresolved external: "symbol struct FOO* gFoo"[/color]

            Well, static means internal linkage, extern means external linkage. You
            have to choose one. If the variable is static, it doesn't make much
            sense to declare it as extern in the header.
            [color=blue]
            > if A.h looks like:
            > extern static FOO* gFoo;
            >
            > it gives:
            > C2159: more than one storage class specified.
            >
            > <blink, blink>
            > How would I do this now? I have a prefedined macro that makes a
            > "static FOO*". And I want to tell all my other .cpp files that it
            > exists in stdafx.h.[/color]

            Why? It's static, so it doesn't exist outside of the .cpp file where
            it's defined.

            Comment

            • Gernot Frisch

              #7
              Re: static extern?

              > Why? It's static, so it doesn't exist outside of the .cpp file where[color=blue]
              > it's defined.[/color]

              Because: That is exactly what I didn't know. :) Thank you,
              Gernot


              Comment

              • Andrey Tarasevich

                #8
                Re: static extern?

                Gernot Frisch wrote:[color=blue]
                > ...
                > Why can't I tell another .cpp file that there is an static variable
                > somewhere else?[/color]

                Because that what this particular use of 'static' is intended to do in
                the first place! It is intended to make the variable "invisible" from
                other translation units. If you want to be able to link to that variable
                from other translation units - give it external linkage.

                --
                Best regards,
                Andrey Tarasevich

                Comment

                • Jack Klein

                  #9
                  Re: static extern?

                  On Mon, 28 Jun 2004 17:53:22 +0800, "Vince Yuan" <shinebean@citi z.net>
                  wrote in comp.lang.c++:

                  First, don't top post.
                  [color=blue]
                  > Try this:
                  >
                  > A.h
                  > static FOO* gFoo;
                  >
                  > A.cpp:
                  > extern static FOO* gFoo=NULL;
                  >
                  > Vince[/color]

                  Second, this is complete nonsense.

                  --
                  Jack Klein
                  Home: http://JK-Technology.Com
                  FAQs for
                  comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
                  comp.lang.c++ http://www.parashift.com/c++-faq-lite/
                  alt.comp.lang.l earn.c-c++

                  Comment

                  • Howard

                    #10
                    Re: static extern?


                    "Rolf Magnus" <ramagnus@t-online.de> wrote in message
                    news:cbp37j$ln6 $04$1@news.t-online.com...[color=blue]
                    > Gernot Frisch wrote:
                    >[color=green]
                    > > Problem:
                    > >
                    > > A.cpp:
                    > > ------
                    > > static FOO* gFoo=NULL;
                    > >
                    > > A.h
                    > > extern FOO* gFoo;
                    > >
                    > > gives: L2001 - unresolved external: "symbol struct FOO* gFoo"[/color]
                    >
                    > Well, static means internal linkage, extern means external linkage. You
                    > have to choose one. If the variable is static, it doesn't make much
                    > sense to declare it as extern in the header.
                    >[/color]

                    <rant>

                    You know, I can't for the life of me figure out why "static" was used to
                    mean internal linkage in this case. I'd think "intern" or something similar
                    would be much more logical, and simply eliminate the use of "static" for
                    non-member functions and variables. I know this is not the forum for asking
                    "why", but it really bugs me to have a keyword that has no apparent relation
                    to its use. One would think that "static" should have the same (or at least
                    similar) meaning in this context as it does in member functions and
                    variables. And since it makes no sense to try to apply the same meaning in
                    this case as in the member (or local variable) case, it really just
                    shouldn't exist at all here. "But that's just my opinion...I could be
                    wrong."

                    </rant>

                    -Howard





                    Comment

                    • Rolf Magnus

                      #11
                      Re: static extern?

                      Howard wrote:
                      [color=blue][color=green]
                      >> Well, static means internal linkage, extern means external linkage.
                      >> You have to choose one. If the variable is static, it doesn't make
                      >> much sense to declare it as extern in the header.
                      >>[/color]
                      >
                      > <rant>
                      >
                      > You know, I can't for the life of me figure out why "static" was used
                      > to
                      > mean internal linkage in this case. I'd think "intern" or something
                      > similar would be much more logical, and simply eliminate the use of
                      > "static" for non-member functions and variables.[/color]

                      I think they didn't want to introduce a new keyword. Also, they wanted
                      to remain compatible to C, which alread had defined it this way. And
                      actionally the meaning that static has within a class definition is the
                      youngest one.
                      [color=blue]
                      > I know this is not the forum for asking "why", but it really bugs me
                      > to have a keyword that has no apparent relation to its use.[/color]

                      I had more problems with the syntax they chose for pure virtual
                      functions. "Initializi ng" a function declaration with 0 looks very odd
                      to me. But again, I suspect they didn't want to add a new keyword
                      "pure".
                      [color=blue]
                      > One would think that "static" should have the same (or at
                      > least similar) meaning in this context as it does in member functions
                      > and variables.[/color]

                      It depends on how you look at it. If you look at it the right way,
                      static has more or less always the same meaning.
                      Within a class, static means that the function or variable is only there
                      once, for the class, not for each instance of it. Same for a local
                      static variable, if you count calling a function as "instantiat ing" it.
                      Well, and a static variable/function on namespace scope is there for
                      this one translation unit. It always has a meaning similar to "there is
                      only one".

                      Comment

                      Working...