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 "C" question.

extern "C" question.

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
5 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.
  • L Offline
    L Offline
    Link2600
    wrote on last edited by
    #1

    If C++ already supports the C Standard Library, why do we need to declare extern "C" when using C-linkage? When do we use extern "C"? Is this compiler-specific feature? Or is it defined in the standard ANSI C++? On MSDN, it says Microsoft C++ supports the strings "C" and "C++" in the string-literal field. However, I have never seen something like extern "C++". Has anyone seen it? Will we ever need to use extern "C++"? Thanks

    L H 2 Replies Last reply
    0
    • L Link2600

      If C++ already supports the C Standard Library, why do we need to declare extern "C" when using C-linkage? When do we use extern "C"? Is this compiler-specific feature? Or is it defined in the standard ANSI C++? On MSDN, it says Microsoft C++ supports the strings "C" and "C++" in the string-literal field. However, I have never seen something like extern "C++". Has anyone seen it? Will we ever need to use extern "C++"? Thanks

      L Offline
      L Offline
      Lim Bio Liong
      wrote on last edited by
      #2

      Hello Alex, The extern "C" declaration is used to tell the C++ compiler not to mangle the symbol which is being declared. When the C++ compiler encounters a function name like "DisplayCurrentThreadId()" which has not been declared as extern "C", it will emit a symbol for it which will look like the following : ?DisplayCurrentThreadId@@YAXXZ The actual symbol produced depends on the compiler used (the above was produced by VC++ 6.0). This symbol will be used in the resulting OBJ file for linking purposes. C++ has a variety of reasons for symbol name mangling, of course. But the reason for the mangling of function name (both class functions and global ones) is to enable function name overloading (i.e. using the same function name with different parameter types). If extern "C" was used to declare the function, the symbol produced for it could be : _DisplayCurrentThreadId This depends on the compiler used. But the bottom line is that no function name overloading will be allowed (since the C language does not support this, hence extern "C"). Hope this helps. Best Regards, Bio.

      L 1 Reply Last reply
      0
      • L Lim Bio Liong

        Hello Alex, The extern "C" declaration is used to tell the C++ compiler not to mangle the symbol which is being declared. When the C++ compiler encounters a function name like "DisplayCurrentThreadId()" which has not been declared as extern "C", it will emit a symbol for it which will look like the following : ?DisplayCurrentThreadId@@YAXXZ The actual symbol produced depends on the compiler used (the above was produced by VC++ 6.0). This symbol will be used in the resulting OBJ file for linking purposes. C++ has a variety of reasons for symbol name mangling, of course. But the reason for the mangling of function name (both class functions and global ones) is to enable function name overloading (i.e. using the same function name with different parameter types). If extern "C" was used to declare the function, the symbol produced for it could be : _DisplayCurrentThreadId This depends on the compiler used. But the bottom line is that no function name overloading will be allowed (since the C language does not support this, hence extern "C"). Hope this helps. Best Regards, Bio.

        L Offline
        L Offline
        Link2600
        wrote on last edited by
        #3

        Bio, Thanks for your reply. I have one more question, does the term extern "C" has anything to do with the storage specifier extern? Now that I understand name mangling, I wonder if extern "C" is a C++ language feature, or is it compilers' feature?

        T 1 Reply Last reply
        0
        • L Link2600

          Bio, Thanks for your reply. I have one more question, does the term extern "C" has anything to do with the storage specifier extern? Now that I understand name mangling, I wonder if extern "C" is a C++ language feature, or is it compilers' feature?

          T Offline
          T Offline
          toxcct
          wrote on last edited by
          #4

          Alex Ngai wrote: does the term extern "C" has anything to do with the storage specifier extern? no, not at all. Alex Ngai wrote: I wonder if extern "C" is a C++ language feature, or is it compilers' feature? C++ standard specification.


          TOXCCT >>> GEII power
          [toxcct][VisualCalc]

          1 Reply Last reply
          0
          • L Link2600

            If C++ already supports the C Standard Library, why do we need to declare extern "C" when using C-linkage? When do we use extern "C"? Is this compiler-specific feature? Or is it defined in the standard ANSI C++? On MSDN, it says Microsoft C++ supports the strings "C" and "C++" in the string-literal field. However, I have never seen something like extern "C++". Has anyone seen it? Will we ever need to use extern "C++"? Thanks

            H Offline
            H Offline
            Henry miller
            wrote on last edited by
            #5

            extern C++ is defined in the standard to mean do whatever the compilers default C++ is. since this is the default it is almost never used.

            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