{"id":111650,"date":"2025-10-03T07:00:00","date_gmt":"2025-10-03T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=111650"},"modified":"2025-10-03T12:11:56","modified_gmt":"2025-10-03T19:11:56","slug":"20251003-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20251003-00\/?p=111650","title":{"rendered":"Can we get weak functions for static linking? The Visual C++ compiler says &#8220;We have weak functions at home&#8221;"},"content":{"rendered":"<p>The ELF object file has this thing called &#8220;weak functions&#8221;. These are functions present in a library that are used by the linker to resolve a symbol only if the main program doesn&#8217;t provide a resolution for them.<\/p>\n<p>The Visual C++ compiler doesn&#8217;t have &#8220;weak functions&#8221; in this sense, but that doesn&#8217;t mean that equivalent functionality doesn&#8217;t exist. It&#8217;s there, just under a different paradigm.<\/p>\n<p>You can get the same effect as static linking weak functions by taking advantage of the classical model for linking: <a title=\"Understanding the classical model for linking: You can override an LIB with another LIB, and a LIB with an OBJ, but you can't override an OBJ\" href=\" https:\/\/devblogs.microsoft.com\/oldnewthing\/20130109-00\/?p=5613\"> You can override a LIB with an OBJ or another LIB<\/a>.<\/p>\n<p>What you do is you put your fallback definition in a library. When the linker needs the function, it starts by looking in the OBJ files, which are the files provided by the main program. If it&#8217;s not found there, then the linker goes through the library files in order, and eventually it finds the definition in your library.<\/p>\n<p>Ta da, you have a function definition in your library which can be overridden by the application.<\/p>\n<p>We used this trick some time ago when we <a title=\"Using the classical model for linking to provide unit test overrides\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20250416-00\/?p=111077\"> created extension points for unit tests to override functionality<\/a>.<\/p>\n<p><b>Bonus chatter<\/b>: Weak functions for static linking is different from weak functions for dynamic linking. <a title=\"Why not use weak linking to solve the retargetable library problem?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160317-00\/?p=93173\"> Windows does not support weak functions from dynamically-linked libraries<\/a>. You can fake it by using delay-load instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s already there, just under a different paradigm.<\/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-111650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It&#8217;s already there, just under a different paradigm.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111650","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=111650"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/111650\/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=111650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=111650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=111650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}