{"id":102546,"date":"2019-06-06T07:00:00","date_gmt":"2019-06-06T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102546"},"modified":"2023-11-20T18:30:49","modified_gmt":"2023-11-21T02:30:49","slug":"20190606-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190606-00\/?p=102546","title":{"rendered":"Why does my C++\/WinRT project get errors of the form &quot;Unresolved external symbol <CODE>void* __cdecl winrt_<WBR>make_<WBR>YourNamespace_<WBR>YourClass(void)<\/CODE>&quot;?"},"content":{"rendered":"<p>So your <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/uwp\/cpp-and-winrt-apis\/\"> C++\/WinRT<\/a> project gets build failures of the form<\/p>\n<pre style=\"white-space: pre-wrap;\">unresolved external symbol \"void * __cdecl winrt_<wbr \/>make_<wbr \/>YourNamespace_<wbr \/>YourClass<wbr \/>(void)\" (?winrt_<wbr \/>make_<wbr \/>YourNamespace_<wbr \/>YourClass<wbr \/>@YAPAXXZ) referenced in function\r\nvoid * __stdcall winrt_<wbr \/>get_<wbr \/>activation_<wbr \/>factory(class std::basic_string_view&lt;wchar_t, struct std::char_traits&lt;wchar_t&gt; &gt; const &amp;)\" (?winrt<wbr \/>_get_<wbr \/>activation_<wbr \/>factory@@YGPAXABV?<wbr \/>$basic_string_view@_WU?<wbr \/>$char_traits@_W@std@@@std@@@Z)\r\n<\/pre>\n<p>What&#8217;s going on, and how do you fix it?<\/p>\n<p>The problem is that you used the <code>-opt<\/code> flag with <code>cppwinrt.exe<\/code>, but didn&#8217;t do the work necessary to support those optimizations.<\/p>\n<p>To each of your implementation files (such as <code>YourClass.cpp<\/code>), add the line<\/p>\n<pre>#include \"YourClass.g.cpp\"\r\n<\/pre>\n<p>If your project defines classes in multiple Windows Runtime namespaces, then the inclusion should be<\/p>\n<pre>#include \"Sub\/Namespace\/YourClass.g.cpp\"\r\n<\/pre>\n<p>If you specified the <code>-prefix<\/code> option, then the inclusion should be<\/p>\n<pre>#include \"Sub.Namespace.YourClass.g.cpp\"\r\n<\/pre>\n<p>(Personally, I put the <code>#include<\/code> directive immediately after the inclusion of the corresponding <code>YourClass.h<\/code> header file.)<\/p>\n<p>In a Visual Studio project, you can enable optimizations by setting<\/p>\n<pre>&lt;CppWinRTOptimized&gt;true&lt;\/CppWinRTOptimized&gt;\r\n<\/pre>\n<p>in your project file.<\/p>\n<p>To turn on dotted prefixes, you can set<\/p>\n<pre>&lt;CppWinRTUsePrefixes&gt;true&lt;\/CppWinRTUsePrefixes&gt;\r\n<\/pre>\n<p>The main optimization enabled by the <code>-opt<\/code> flag in C++\/WinRT 2.0 is bypassing the call to <code>Ro\u00adGet\u00adActivatation\u00adFactory<\/code> if the class is implemented in the same module. Instead, the call goes directly to the implementation. This also removes the need to declare the runtime class in your manifest if it is used only within the module (say, by XAML binding).<\/p>\n<p><b>Bonus reading<\/b>: <a href=\"https:\/\/kennykerr.ca\/2019\/06\/07\/cppwinrt-optimizing-components\/\"> Meet C++\/WinRT 2.0: Optimizing Components<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You enabled optimizations, but there&#8217;s some extra bookkeeping to support those optimizations.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-102546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You enabled optimizations, but there&#8217;s some extra bookkeeping to support those optimizations.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102546","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=102546"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102546\/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=102546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}