cppwinrt: App.g.h Generated Code errors or doesn't match UWP C++/CX Template

After adding the CppWinRT package from NuGet to a blank C++ UWP project, I’m getting compile errors.

Platform: Windows 10 Version 22H2 (OS Build 19045.2193)

Windows Target Platform Version: 10.0.22621.0 Windows Platform Min. Version: 10.0.17763.0

Build started...
1>------ Build started: Project: Issue_CppWinRT, Configuration: Debug x64 ------
1>XamlTypeInfo.g.cpp
1>D:\Public\Issue_CppWinRT\Generated Files\App.xaml.g.hpp(51,1): warning C4447: 'main' signature found without threading model. Consider using 'int main(Platform::Array<Platform::String^>^ args)'.
1>D:\Public\Issue_CppWinRT\Generated Files\MainPage.xaml.g.hpp(25,19): error C2988: unrecognizable template declaration/definition
1>D:\Public\Issue_CppWinRT\Generated Files\MainPage.xaml.g.hpp(25,19): error C2143: syntax error: missing ';' before '<'
1>D:\Public\Issue_CppWinRT\Generated Files\MainPage.xaml.g.hpp(25,10): error C2182: 'MainPageT': this use of 'void' is not valid
1>D:\Public\Issue_CppWinRT\Generated Files\MainPage.xaml.g.hpp(25,19): error C2059: syntax error: '<'
1>D:\Public\Issue_CppWinRT\Generated Files\MainPage.xaml.g.hpp(25,30): error C2039: 'InitializeComponent': is not a member of '`global namespace''
1>D:\Public\Issue_CppWinRT\Generated Files\MainPage.xaml.g.hpp(26,5): error C2143: syntax error: missing ';' before '{'

I created a video about the issue:

https://youtu.be/Oxr_tHbZmZY

Errors compiling with Windows Platform Min. Version: 10.0.18362.0

1>D:\Public\Issue_CppWinRT\App.xaml.h(8,10): fatal  error C1083: Cannot open include file: 'App.g.h': No such file or directory
1>Done building project "Issue_CppWinRT.vcxproj" -- FAILED.

Errors compiling with Windows Platform Min. Version: 10.0.19041.0

1>D:\Public\Issue_CppWinRT\App.xaml.h(8,10): fatal  error C1083: Cannot open include file: 'App.g.h': No such file or directory
1>Done building project "Issue_CppWinRT.vcxproj" -- FAILED.

Repro Project - https://github.com/tgraupmann/Issue_CppWinRT

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (3 by maintainers)

Most upvoted comments

The MainPage class is generated by the XAML compiler. Since you started from a C++/CX project, that is going to put C++/CX in charge: The XAML compiler runs in C++/CX mode, which means that the App and MainPage will be ref classes.

If all you need are strong and weak references, then you don’t need to derive from winrt::implements. Use the native C++/CX types. In C++/CX land, you use a ^ (hat) pointer for a strong reference, and a Platform::WeakReference for a weak reference.

It may well be a compatibility issue with Xaml but I’d need someone from the Xaml team to take a look.

That’s my first include in pch.h.

https://github.com/tgraupmann/ChromeOS_Whisper/blob/main/Cpp_Uwp_ServerBase/pch.h

As soon as I add CppWinRT - 2.0.220929.3 I get problems with the generated XAML.