{"id":7533,"date":"2012-05-25T07:00:00","date_gmt":"2012-05-25T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/05\/25\/the-extern-c-specifier-disables-c-mangling-but-that-doesnt-mean-it-disables-mangling\/"},"modified":"2024-03-28T04:35:07","modified_gmt":"2024-03-28T11:35:07","slug":"the-extern-c-specifier-disables-c-mangling-but-that-doesnt-mean-it-disables-mangling","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120525-00\/?p=7533","title":{"rendered":"The extern &#8220;C&#8221; specifier disables C++ mangling, but that doesn&#8217;t mean it disables mangling"},"content":{"rendered":"<p>The <a href=\"https:\/\/learn.microsoft.com\/en-us\/cpp\/cpp\/dllexport-dllimport?view=msvc-170&#038;redirectedfrom=MSDN\"> MSDN documentation on dllexport<\/a> contains the following enigmatic paragraph, or at least did at the time I wrote this article:<\/p>\n<blockquote class=\"q\"><p> <b>dllexport<\/b> of a C++ function will expose the function with C++ name mangling. If C++ name mangling is not desired, either use a .def file (EXPORTS keyword) or declare the function as extern &#8220;C&#8221;. <\/p><\/blockquote>\n<p> I&#8217;ve seen this sentence misinterpreted as follows:<\/p>\n<blockquote class=\"q\"><p> <b>dllexport<\/b> of a C++ function will expose the function with C++ name mangling. <u>To disable name mangling<\/u> either use a .def file (EXPORTS keyword) or declare the function as extern &#8220;C&#8221;. <\/p><\/blockquote>\n<p> This is an understandable misinterpretation, but it is still a misinterpretation.<\/p>\n<p> The root cause of the misinterpretation is that the author of this documentation was wearing <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110512-00\/?p=10683\"> C++-colored glasses<\/a>. In the author&#8217;s mind, there are only two interesting cases: <\/p>\n<ol>\n<li>C++ name mangling, where all the cool people are, and <\/li>\n<li>everything else, for all the lamers. <\/li>\n<\/ol>\n<p> Here is a precise formulation of the paragraph: <\/p>\n<blockquote class=\"q\"><p> <b>dllexport<\/b> of a C++ function will expose the function with C++ name mangling. If C++ name mangling is not desired, either use a .def file (EXPORTS keyword), <u>which will expose the name without mangling<\/u>, or declare the function as extern &#8220;C&#8221;, <u>which will expose the name with <a href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040108-00\/?p=41163\"> C mangling<\/a><\/u>. <\/p><\/blockquote>\n<p> Here&#8217;s a version of the paragraph that tries to take away the C++-colored glasses. <\/p>\n<blockquote class=\"q\"><p> <b>dllexport<\/b> exposes the function as it is decorated by the compiler. For example, if the function is a C++ function, it will be exposed with C++ name mangling. If the function is a C function, or has been declared as <code>extern \"C\"<\/code>, it will be exposed with C name mangling. To expose the function under its unmangled name (or to expose it via an alternate name), use use a .def file (EXPORTS keyword). <\/p><\/blockquote>\n<p> <b>Behind the scenes<\/b>: To forestall nitpickers, I had to go back to my copy of the C++ standard to make sure I filled in the blank in &#8220;The <code>extern \"C\"<\/code> _________&#8221; correctly. Officially, <code>extern \"C\"<\/code> is a <i>storage class specifier<\/i>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The MSDN documentation on dllexport contains the following enigmatic paragraph, or at least did at the time I wrote this article: dllexport of a C++ function will expose the function with C++ name mangling. If C++ name mangling is not desired, either use a .def file (EXPORTS keyword) or declare the function as extern &#8220;C&#8221;. [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-7533","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The MSDN documentation on dllexport contains the following enigmatic paragraph, or at least did at the time I wrote this article: dllexport of a C++ function will expose the function with C++ name mangling. If C++ name mangling is not desired, either use a .def file (EXPORTS keyword) or declare the function as extern &#8220;C&#8221;. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=7533"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/7533\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=7533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=7533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=7533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}