{"id":19693,"date":"2008-12-29T10:00:00","date_gmt":"2008-12-29T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2008\/12\/29\/undecorating-names-to-see-why-a-function-cant-be-found\/"},"modified":"2008-12-29T10:00:00","modified_gmt":"2008-12-29T10:00:00","slug":"undecorating-names-to-see-why-a-function-cant-be-found","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20081229-00\/?p=19693","title":{"rendered":"Undecorating names to see why a function can&#8217;t be found"},"content":{"rendered":"<p><P>\nHere&#8217;s a problem inspired by actual events.\n<\/P>\n<BLOCKQUOTE CLASS=\"q\">\n<P>\nWhen I build my project, it compiles fine, but it fails during the\nlink step with an unresolved external:\n<\/P>\n<PRE>\nprogram.obj : error LNK2001: unresolved external symbol\n&#8220;public: virtual wchar_t const * __thiscall\nUILibrary::PushButton::GetName(class UILibrary::StringHolder * *)&#8221;\n(?GetName@PushButton@UILibrary@@UAEPB_WPAPAVStringHolder@2@@Z)\n<\/PRE>\n<P>\nThe function I&#8217;m trying to call exists in the source code for\n<CODE>uilibrary.lib<\/CODE>; I&#8217;m looking at it right now.\nAnd the definition in the source code matches the declaration\nin the header file:\n<\/P>\n<PRE>\nnamespace UILibrary {\n &#8230;\n class PushButton {\n public:\n  virtual LPCWSTR GetName(StringHolder **Holder);\n };\n &#8230;\n}\n<\/PRE>\n<P>\nWhy can&#8217;t the linker find it?\n(Other functions in <CODE>uilibrary.lib<\/CODE> link just fine.)\n<\/P>\n<\/BLOCKQUOTE>\n<P>\nIn order to find something, you have to be looking in the right place,\nand the thing you&#8217;re looking for actually needs to be there.\n(And you have to be able to see it when it&#8217;s there.)\nThe first part, looking in the right place, appears to be addressed\nby the parenthetical:\nThe linker is definitely looking in <CODE>uilibrary.lib<\/CODE>\nsince it managed to find other things in that library.\n<\/P>\n<P>\nLet&#8217;s look at the second step, then.\nIs the thing you&#8217;re looking for really there?\nI fired up a little hex editor on <CODE>uilibrary.lib<\/CODE>,\nbut you could use <CODE>strings<\/CODE> or, if you really want\nto get fancy, <CODE>link \/dump \/headers<\/CODE>.\nI went looking for &#8220;GetName@PushButton&#8221; to see if the member\nfunction was actually in the library.\n<\/P>\n<P>\nAnd yup, the function is there.\nBut it looks slightly different:\n<CODE>?GetName@PushButton@UILibrary@@UAEPBGPAPAVStringHolder@2@@Z<\/CODE>.\n(See if you can spot the difference.)\nAha, the symbol couldn&#8217;t be found because it indeed doesn&#8217;t exist!\nWhat does exist is something that superficially resembles the\nsymbol we want, but which has different decoration.\nWe ask the\n<A HREF=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/5x49w699(VS.80).aspx\">\n<CODE>undname<\/CODE><\/A> program to convert this name\ninto something a bit more readable:\n<\/P>\n<PRE>\nC:\\&gt; undname ?GetName@PushButton@UILibrary@@UAEPBGPAPAVStringHolder@2@@Z\npublic: virtual unsigned short const * __thiscall\nUILibrary::PushButton::GetName(class UILibrary::StringHolder * *)\n<\/PRE>\n<P>\nLooking carefully at the two functions, we see that the\ndifference is that the one that <CODE>program.obj<\/CODE> \nis looking for has a return type of <CODE>wchar_t const *<\/CODE>,\nwhereas the one in the library returns a\n<CODE>unsigned short const *<\/CODE>.\n<\/P>\n<P>\nAt this point the answer is obvious.\nThe library was compiled with the <CODE>\/Zc:wchar_t-<\/CODE>* flag,\nwhich disables <CODE>wchar_t<\/CODE> as a native type.\nWhen that happens, the Windows header files gives the\n<CODE>wchar_t<\/CODE> symbol the definition\n<CODE>typedef unsigned short wchar_t;<\/CODE>\nOn the other hand, the customer&#8217;s project was being compiled\nwithout that switch, in which case <CODE>wchar_t<\/CODE> is\na native type and not an alias for <CODE>unsigned short<\/CODE>.\n<\/P>\n<P>\nNow you know enough to solve this customer&#8217;s problem,\nwhich is very similar to the previous one:\n<\/P>\n<BLOCKQUOTE CLASS=\"q\">\n<P>\nWhen I build my project, it compiles fine, but it fails during the\nlink step with an unresolved external:\n<\/P>\n<PRE>\nprogram.obj : error LNK2019: unresolved external symbol\n&#8220;long __cdecl UILibrary::Initialize(bool)&#8221;\n(?Initialize@UILibrary@@YAJ_N@Z)\n<\/PRE>\n<\/BLOCKQUOTE>\n<P>\nThe function as it exists in the library undecorates as follows:\n<\/P>\n<PRE>\nlong __stdcall UILibrary::Initialize(bool)\n<\/PRE>\n<P>\n<B>Note<\/B>\n<\/P>\n<P>\nThe <CODE>undname<\/CODE> program and the <CODE>\/Zc:wchar_t-<\/CODE>\nswitches are specific to the Microsoft Visual C++ compiler.\nNaturally, if you use a different compiler, you should use the\nutility or command line switch appropriate to your compiler.\nIn particular, if you use the Visual Studio development environment,\nI&#8217;m told (but have not tried it myself)\nthat the switch you&#8217;re looking for is called\n&#8220;Treat wchar_t as a built-in type&#8221; on the &#8220;C\/C++ Language&#8221;\nproperty page.\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a problem inspired by actual events. When I build my project, it compiles fine, but it fails during the link step with an unresolved external: program.obj : error LNK2001: unresolved external symbol &#8220;public: virtual wchar_t const * __thiscall UILibrary::PushButton::GetName(class UILibrary::StringHolder * *)&#8221; (?GetName@PushButton@UILibrary@@UAEPB_WPAPAVStringHolder@2@@Z) The function I&#8217;m trying to call exists in the source code [&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-19693","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Here&#8217;s a problem inspired by actual events. When I build my project, it compiles fine, but it fails during the link step with an unresolved external: program.obj : error LNK2001: unresolved external symbol &#8220;public: virtual wchar_t const * __thiscall UILibrary::PushButton::GetName(class UILibrary::StringHolder * *)&#8221; (?GetName@PushButton@UILibrary@@UAEPB_WPAPAVStringHolder@2@@Z) The function I&#8217;m trying to call exists in the source code [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/19693","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=19693"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/19693\/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=19693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=19693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=19693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}