{"id":44753,"date":"2015-02-04T07:00:00","date_gmt":"2015-02-04T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2015\/02\/04\/if-you-wonder-why-a-function-cant-be-found-one-thing-to-check-is-whether-the-function-exists-in-the-first-place\/"},"modified":"2015-02-04T07:00:00","modified_gmt":"2015-02-04T07:00:00","slug":"if-you-wonder-why-a-function-cant-be-found-one-thing-to-check-is-whether-the-function-exists-in-the-first-place","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20150204-00\/?p=44753","title":{"rendered":"If you wonder why a function can&#039;t be found, one thing to check is whether the function exists in the first place"},"content":{"rendered":"<p>\nOne of my colleagues was frustrated trying to get some code to build.\n&#8220;Is there something strange about linking variadic functions?\nBecause I keep getting an unresolved external error for the function,\nbut if I move the function definition to the declaration point,\nthen everything works fine.&#8221;\n<\/p>\n<pre>\n\/\/ blahblah.h\n... other declarations ...\nvoid LogWidget(Widget* widget, const char* format, ...);\n...\n\/\/ widgetstuff.cpp\n...\n#include \"blahblah.h\"\n...\n\/\/ some code that calls LogWidget\nvoid foo(Widget* widget)\n{\n LogWidget(widget, \"starting foo\");\n ...\n}\n\/\/ and then near the end of the file\nvoid LogWidget(Widget* widget, const char* format, ...)\n{\n    ... implementation ...\n}\n...\n<\/pre>\n<p>\n&#8220;With the above code, the linker complains that\n<code>Log&shy;Widget<\/code> cannot be found.\nBut if I move the implementation of\n<code>Log&shy;Widget<\/code> to the top of the file,\nthen everything builds fine.&#8221;\n<\/p>\n<pre>\n\/\/ widgetstuff.cpp\n...\n#include \"blahblah.h\"\n...\n\/\/ move the code up here\nvoid LogWidget(Widget* widget, const char* format, ...)\n{\n    ... implementation ...\n}\n\/\/ some code that calls LogWidget\nvoid foo(Widget* widget)\n{\n LogWidget(widget, \"starting foo\");\n ...\n}\n...\n<\/pre>\n<\/p>\n<p>&#8220;I tried putting an explicit calling convention in the\ndeclaration,\nI tried using <code>extern \"C\"<\/code>,\nnothing seems to help.&#8221;\n<\/p>\n<p>\nWe looked at the resulting object file and observed that\nin the case where the error occurred,\nthere was an external reference to\n<code>Log&shy;Widget<\/code> but no definition.\nI asked,\n&#8220;Is the definition of the function\n<code>#ifdef<\/code>&#8216;d out by mistake?\nYou can use\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2008\/04\/10\/8370479.aspx\">\nthis technique<\/a>\nto find out.&#8221;\n<\/p>\n<p>\nThat was indeed the problem.\nThe definition of the function was inside some sort of\n<code>#ifdef<\/code> that prevented it from being compiled.\n<\/p>\n<p>\nSometimes, the reason a function cannot be found is that it doesn&#8217;t\nexist in the first place.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of my colleagues was frustrated trying to get some code to build. &#8220;Is there something strange about linking variadic functions? Because I keep getting an unresolved external error for the function, but if I move the function definition to the declaration point, then everything works fine.&#8221; \/\/ blahblah.h &#8230; other declarations &#8230; void LogWidget(Widget* [&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":[26],"class_list":["post-44753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-other"],"acf":[],"blog_post_summary":"<p>One of my colleagues was frustrated trying to get some code to build. &#8220;Is there something strange about linking variadic functions? Because I keep getting an unresolved external error for the function, but if I move the function definition to the declaration point, then everything works fine.&#8221; \/\/ blahblah.h &#8230; other declarations &#8230; void LogWidget(Widget* [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44753","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=44753"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/44753\/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=44753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=44753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=44753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}