Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. extern sample in MSDN

extern sample in MSDN

Scheduled Pinned Locked Moved C / C++ / MFC
c++comquestion
16 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G George_George

    Hi Don, I also think it is correct. But do we achieve special benefits if we use extern in the same compile unit? regards, George

    S Offline
    S Offline
    Stephen Hewitt
    wrote on last edited by
    #4

    No. The reason for extern is to differentiate between a declaration and a definition: int num; // Is this a definition or a declaration? In this case the compiler assumes it’s a definition. If you want to share num between two compilation units this will not do the trick and will result in a linker error – there will be two variables with the same name. extern int num; // This is a declaration. Now this referrers to a “num” defined elsewhere.

    Steve

    G 1 Reply Last reply
    0
    • S Stephen Hewitt

      No. The reason for extern is to differentiate between a declaration and a definition: int num; // Is this a definition or a declaration? In this case the compiler assumes it’s a definition. If you want to share num between two compilation units this will not do the trick and will result in a linker error – there will be two variables with the same name. extern int num; // This is a declaration. Now this referrers to a “num” defined elsewhere.

      Steve

      G Offline
      G Offline
      George_George
      wrote on last edited by
      #5

      Thanks Steve, In the sample, even if you do not use why we need to write statement, extern int i? i is global and we can use it anywhere in the same compile unit (cpp file). regards, George

      S 1 Reply Last reply
      0
      • G George_George

        Thanks Steve, In the sample, even if you do not use why we need to write statement, extern int i? i is global and we can use it anywhere in the same compile unit (cpp file). regards, George

        S Offline
        S Offline
        Stephen Hewitt
        wrote on last edited by
        #6

        As I said, extern is used to indicate that a construct is a declaration and not a definition. With functions no such mechanism is needed: if the function has a body it’s a definition and if not it’s a declaration:

        int void FooBar(); // This is a declaration (no body).
        int void FooBar() // This is a definition (has body).
        {
        // Do stuff…
        }

        If you want to use the “FooBar” function in another compilation unit (from where it’s defined) you need to make sure to include its declaration. This is usually done by including a header file. It’s an error to define the same function twice in two separate compilation units however and attempting to do so will result in a link error. Since variables don’t have bodies this technique isn’t usable, thus the extern keyword:

        int FooBar; // This is a definition.
        extern int FooBar; // This is a declaration.

        Steve

        G 1 Reply Last reply
        0
        • S Stephen Hewitt

          As I said, extern is used to indicate that a construct is a declaration and not a definition. With functions no such mechanism is needed: if the function has a body it’s a definition and if not it’s a declaration:

          int void FooBar(); // This is a declaration (no body).
          int void FooBar() // This is a definition (has body).
          {
          // Do stuff…
          }

          If you want to use the “FooBar” function in another compilation unit (from where it’s defined) you need to make sure to include its declaration. This is usually done by including a header file. It’s an error to define the same function twice in two separate compilation units however and attempting to do so will result in a link error. Since variables don’t have bodies this technique isn’t usable, thus the extern keyword:

          int FooBar; // This is a definition.
          extern int FooBar; // This is a declaration.

          Steve

          G Offline
          G Offline
          George_George
          wrote on last edited by
          #7

          Thanks Steve, How about this, extern int i = 100; a definition or declaration, why? regards, George

          S 1 Reply Last reply
          0
          • G George_George

            Thanks Steve, How about this, extern int i = 100; a definition or declaration, why? regards, George

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #8

            From section 3.1.2 of the C++ standard:  “A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (7.5) and neither an initializer nor a function-body, it declares a static data member in a class declaration (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), or a using-directive (7.3.4).”

            Steve

            G 1 Reply Last reply
            0
            • S Stephen Hewitt

              From section 3.1.2 of the C++ standard:  “A declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification (7.5) and neither an initializer nor a function-body, it declares a static data member in a class declaration (9.4), it is a class name declaration (9.1), or it is a typedef declaration (7.1.3), a using-declaration (7.3.3), or a using-directive (7.3.4).”

              Steve

              G Offline
              G Offline
              George_George
              wrote on last edited by
              #9

              Thanks Steve, 1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right? 2. What is " linkage-specification (7.5)"? regards, George

              S 1 Reply Last reply
              0
              • G George_George

                Hello everyone, I think in practical experience, extern is useful only when we have more than one compile unit (cpp file) and making cross-reference between compile units. In the MSDN extern sample, http://msdn2.microsoft.com/en-us/library/0603949d.aspx It only uses one source file (compile unit) to demonstrate the usage of extern, is it correct and practical? thanks in advance, George

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #10

                I think this[^] article is helpful for you.

                G 1 Reply Last reply
                0
                • H Hamid Taebi

                  I think this[^] article is helpful for you.

                  G Offline
                  G Offline
                  George_George
                  wrote on last edited by
                  #11

                  Thanks Hamid, But it does not cover the case when using extern to qualify a variable which is defined in the same compile unit. :-) regards, George

                  S 1 Reply Last reply
                  0
                  • G George_George

                    Thanks Steve, 1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right? 2. What is " linkage-specification (7.5)"? regards, George

                    S Offline
                    S Offline
                    Stephen Hewitt
                    wrote on last edited by
                    #12

                    George_George wrote:

                    1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right?

                    No, because of the "and neither an initializer nor a function-body" clause; the example you gave does have an initializer.

                    extern int i; // This is a declaration.
                    extern int i = 100; // This contains an initializer and thus is a definition (and a declaration).

                    George_George wrote:

                    2. What is " linkage-specification (7.5)"?

                    extern "C" is an example.

                    Steve

                    G 1 Reply Last reply
                    0
                    • G George_George

                      Thanks Hamid, But it does not cover the case when using extern to qualify a variable which is defined in the same compile unit. :-) regards, George

                      S Offline
                      S Offline
                      Stephen Hewitt
                      wrote on last edited by
                      #13

                      Doing so is poinless and possibly misleading, but harmless.

                      Steve

                      1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        George_George wrote:

                        1. So, extern int i = 100; should be declaration and matches the rule, " it contains the extern specifier (7.1.1)", right?

                        No, because of the "and neither an initializer nor a function-body" clause; the example you gave does have an initializer.

                        extern int i; // This is a declaration.
                        extern int i = 100; // This contains an initializer and thus is a definition (and a declaration).

                        George_George wrote:

                        2. What is " linkage-specification (7.5)"?

                        extern "C" is an example.

                        Steve

                        G Offline
                        G Offline
                        George_George
                        wrote on last edited by
                        #14

                        Thanks Stephen, Any special advantage/restrictions/functions we could have when writing extern int i = 3 other than int i = 3? (I think you mean extern int i = 3 has the same meaning as int i = 3, which is definition with initialization. right?) regards, George

                        S 1 Reply Last reply
                        0
                        • G George_George

                          Thanks Stephen, Any special advantage/restrictions/functions we could have when writing extern int i = 3 other than int i = 3? (I think you mean extern int i = 3 has the same meaning as int i = 3, which is definition with initialization. right?) regards, George

                          S Offline
                          S Offline
                          Stephen Hewitt
                          wrote on last edited by
                          #15

                          There is no reason to do so (use extern) in such cases. Doing so does neither harm nor good; the two constructs are equivalent. I wouldn’t in cases like that however as it could lead to confusion.

                          Steve

                          G 1 Reply Last reply
                          0
                          • S Stephen Hewitt

                            There is no reason to do so (use extern) in such cases. Doing so does neither harm nor good; the two constructs are equivalent. I wouldn’t in cases like that however as it could lead to confusion.

                            Steve

                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #16

                            Thanks Steve, My question is answered. regards, George

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups