extern "C"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • George2
    New Member
    • Dec 2007
    • 200

    extern "C"

    Hello everyone,


    Just to confirm what is the most correct way beyond a just working function.

    1.

    We need to add extern "C" to both variable/function definition/declaration? Or only need to add to the variable/function declaration?

    2.

    How about extern? Declaration only or both declaration and definition are required?

    BTW: previously, I only add to declaration, but after reading more and more code which add to both declaration and definition, I come to here to ask this question.


    thanks in advance,
    George
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Do you know what extern "C" is for??

    Comment

    • RedSon
      Recognized Expert Expert
      • Jan 2007
      • 4980

      #3
      Originally posted by weaknessforcats
      Do you know what extern "C" is for??
      I think we can all answer that for the OP...

      Comment

      • George2
        New Member
        • Dec 2007
        • 200

        #4
        Sure, weaknessforcats . Used for stopping mangling name for function and variable, any comments to my original question?


        Originally posted by weaknessforcats
        Do you know what extern "C" is for??

        regards,
        George

        Comment

        • George2
          New Member
          • Dec 2007
          • 200

          #5
          Thanks RedSon!


          Any comments to my post #4?

          Originally posted by RedSon
          I think we can all answer that for the OP...

          regards,
          George

          Comment

          • RedSon
            Recognized Expert Expert
            • Jan 2007
            • 4980

            #6
            Originally posted by George2
            Sure, weaknessforcats . Used for stopping mangling name for function and variable, any comments to my original question?





            regards,
            George
            Actually it is more complicated then that. It's true that "extern c" does prevent mangling but that is a side effect of c linking. What compiler are you using?

            Comment

            • hdanw
              New Member
              • Feb 2008
              • 61

              #7
              Use the extern keyword so that they can be linked outside of its normal frame of reference.

              The "c" tells the compiler how to push information on the stack.

              The keyword should be added to the Definition of functions, so they can be called by other files.

              And declaring extern on a declaration means that the actual declaration exists outside of this file. And should be visible to all linked object files.

              Code:
              // myfile.cpp
              int globalint
              
              extern myfunc()
              {
                // do some stuff
              }
              Code:
              // myfile.hpp
              
              extern int globalint;
              extern myfunc();
              Code:
              // other.cpp
              
              #include "myfile.hpp"
              
              globalint = 10;
              myfunc();

              Comment

              • George2
                New Member
                • Dec 2007
                • 200

                #8
                Thanks RedSon,


                I am using Visual Studio 2008 to develop C/C++. Any advice about whether to add extern "C" to declaration only or both declaration and definition?

                Originally posted by RedSon
                Actually it is more complicated then that. It's true that "extern c" does prevent mangling but that is a side effect of c linking. What compiler are you using?

                regards,
                George

                Comment

                • George2
                  New Member
                  • Dec 2007
                  • 200

                  #9
                  Thanks hdanw,


                  Please review whether I am correct.

                  1. We should add extern to declaration, so what we know we are referring a function/variale from other compile unit;
                  2. We should also add extern to definition so that this function/variale is visible to other compile unit. In this case, extern is on the other side of static.

                  Right?


                  regards,
                  George

                  Originally posted by hdanw
                  Use the extern keyword so that they can be linked outside of its normal frame of reference.

                  The "c" tells the compiler how to push information on the stack.

                  The keyword should be added to the Definition of functions, so they can be called by other files.

                  And declaring extern on a declaration means that the actual declaration exists outside of this file. And should be visible to all linked object files.

                  Code:
                  // myfile.cpp
                  int globalint
                  
                  extern myfunc()
                  {
                    // do some stuff
                  }
                  Code:
                  // myfile.hpp
                  
                  extern int globalint;
                  extern myfunc();
                  Code:
                  // other.cpp
                  
                  #include "myfile.hpp"
                  
                  globalint = 10;
                  myfunc();

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by George2
                    I am using Visual Studio 2008 to develop C/C++. Any advice about whether to add extern "C" to declaration only or both declaration and definition?
                    I just did a Google on "extern "C" " and got 309,000 hits.

                    Maybe you could read a few of these pages. What you are asking is covered in any C++ reference material.

                    You are supposed to post questions when you are stuck in the code and not as a substitute for doing your own research.

                    Next:

                    All function prototypes and definitions have a default storage class specifier of extern. That is, the functions have external linkage. This means you do not need to specify extern. You can override the default with static. However, using the "C" qualifier will require the extern spcifier.

                    Comment

                    • RedSon
                      Recognized Expert Expert
                      • Jan 2007
                      • 4980

                      #11
                      Educate yourself ...

                      Comment

                      • George2
                        New Member
                        • Dec 2007
                        • 200

                        #12
                        Thanks weaknessforcats ,


                        I think we should add extern "C" to both declaration and definition, right?

                        http://www.glenmccl.co m/ansi028.htm

                        Originally posted by weaknessforcats
                        I just did a Google on "extern "C" " and got 309,000 hits.

                        Maybe you could read a few of these pages. What you are asking is covered in any C++ reference material.

                        You are supposed to post questions when you are stuck in the code and not as a substitute for doing your own research.

                        Next:

                        All function prototypes and definitions have a default storage class specifier of extern. That is, the functions have external linkage. This means you do not need to specify extern. You can override the default with static. However, using the "C" qualifier will require the extern spcifier.

                        regards,
                        George

                        Comment

                        • George2
                          New Member
                          • Dec 2007
                          • 200

                          #13
                          Hi RedSon,


                          I do not think the same in the MSDN page is good enough. When demonstrating extern, it should use more than one cpp files (more than one compile unit) right?

                          Code:
                          // specifying_linkage1.cpp
                          int i = 1;
                          void other();
                          
                          int main() {
                             // Reference to i, defined above:
                             extern int i;
                          }
                          
                          void other() {
                             // Address of global i assigned to pointer variable:
                             static int *external_i = &i;
                          
                             // i will be redefined; global i no longer visible:
                             // int i = 16;
                          }
                          Originally posted by RedSon

                          regards,
                          George

                          Comment

                          • RedSon
                            Recognized Expert Expert
                            • Jan 2007
                            • 4980

                            #14
                            Originally posted by George2
                            Hi RedSon,


                            I do not think the same in the MSDN page is good enough. When demonstrating extern, it should use more than one cpp files (more than one compile unit) right?

                            regards,
                            George
                            The point of providing the link was to give you a starting point into your investigation into the extern keyword. Did you follow any of the links in the see also section?

                            Comment

                            • George2
                              New Member
                              • Dec 2007
                              • 200

                              #15
                              Sure, RedSon.


                              Any comments to my post #13? I think to demonstrate the usage of extern, we should use more than one compile unit (cpp file) to demonstrate, right?

                              Originally posted by RedSon
                              The point of providing the link was to give you a starting point into your investigation into the extern keyword. Did you follow any of the links in the see also section?

                              regards,
                              George

                              Comment

                              Working...