{"id":38543,"date":"2004-07-06T07:01:00","date_gmt":"2004-07-06T07:01:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/07\/06\/diagnosing-a-problem-with-calling-conventions\/"},"modified":"2020-08-26T08:49:56","modified_gmt":"2020-08-26T15:49:56","slug":"diagnosing-a-problem-with-calling-conventions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040706-00\/?p=38543","title":{"rendered":"Diagnosing a problem with calling conventions"},"content":{"rendered":"<p><a href=\"https:\/\/web.archive.org\/web\/20080201075542\/http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/01\/08\/48616.aspx#143025\"> A commenter asks for help with an unresolved external<\/a>. One of my goals is to give you more insight into how things work so you can solve problems yourself. This particular problem &#8211; resolving the error &#8220;Undefined symbol: &#8216;__stdcall(0) pl_pvcam_init (_pl_pvcam_init@0)&#8217; referenced from &#8216;_main&#8217; in Acquisition.c:15&#8221; is one example of something you can solve with the tips you&#8217;ve already learned.<\/p>\n<p>First, let&#8217;s look at the unresolved external itself. &#8220;_pl_pvcam_init@0&#8221;. <a href=\"https:\/\/web.archive.org\/web\/20080201075542\/http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/01\/08\/48616.aspx\"> From the article this comment was posted to<\/a>, you can see that the leading underscore and trailing @0 indicate that the function uses the __stdcall calling convention. (This is confirmed by the linker&#8217;s undecoration of the name.)<\/p>\n<p>So your function &#8220;_main&#8221; wants the function pl_pvcam_init with the __stdcall calling convention. But it&#8217;s not found in the library even though you linked to it.<\/p>\n<p>If you look inside the library itself, you&#8217;ll find the desired symbols with some decoration. Decode that decoration. (<a href=\"https:\/\/web.archive.org\/web\/20080201075542\/http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/17\/74812.aspx#74862\">My psychic powers<\/a> tell me that when you do, you&#8217;ll find that the decoration is &#8220;_pl_pvcam_init&#8221;, which is the __cdecl calling convention.)<\/p>\n<p>So now you see the problem. Your code is calling with the __stdcall calling convention, but the function actually uses the __cdecl calling convention. The calling conventions don&#8217;t match up, so the linkage fails.<\/p>\n<p>The solution, of course, is to fix the declaration of the pl_pvcam_init function in the header file to specify the correct calling convention. My psychic powers tell me that the header file doesn&#8217;t specify any calling convention at all, which puts it at the mercy of the ambient calling convention for your project, which appears to be __stdcall. But the author of the header file expected __cdecl to be the default calling convention.<\/p>\n<p>Put explicit calling conventions on the functions and you should be all set.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Putting together some skills you&#8217;ve already learned.<\/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-38543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Putting together some skills you&#8217;ve already learned.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38543","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=38543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/38543\/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=38543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=38543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=38543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}