microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.3k stars 676 forks source link

Build break when C++ Create and reference a WinUI 3 Component library using WinAppSDK 1.4.230913002 #8925

Closed AnnYang01 closed 10 months ago

AnnYang01 commented 1 year ago

Describe the bug

Build break when C++ Create and reference a WinUI 3 Component library using WinAppSDK 1.4.230913002. Severity Code Description Project File Line Suppression State Details

Error   C2079   'winrt::impl::producer<D,winrt::Microsoft::UI::Xaml::Markup::IComponentConnector,void>::vtable' uses undefined struct 'winrt::impl::produce<D,I>'
        with
        [
            D=winrt::ThermometerWRC::implementation::UserControl
        ]   ThermometerWRC  C:\Users\xx\source\re\ThermometerWRC\ThermometerWRC\Generated Files\winrt\base.h    7001        
Error   C3083   'Foundation': the symbol to the left of a '::' must be a type   ThermometerWRC  C:\Users\xx\source\re\ThermometerWRC\ThermometerWRC\Generated Files\winrt\microsoft.ui.xaml.markup.h    1278        
Error   C2061   syntax error: identifier 'IInspectable' ThermometerWRC  C:\Users\xx\source\re\ThermometerWRC\ThermometerWRC\Generated Files\winrt\microsoft.ui.xaml.markup.h    1278        
Error   C3083   'Foundation': the symbol to the left of a '::' must be a type   ThermometerWRC  C:\Users\xx\source\re\ThermometerWRC\ThermometerWRC\Generated Files\winrt\microsoft.ui.xaml.markup.h    1291        
Error   C2061   syntax error: identifier 'IInspectable' ThermometerWRC  C:\Users\xx\source\re\ThermometerWRC\ThermometerWRC\Generated Files\winrt\microsoft.ui.xaml.markup.h    1291    

Steps to reproduce the bug

1.WinAppSdk 1.4.1 Insertion into VS Pipelines - Run main-34126.236 (visualstudio.com) 2.Create a C++ Blank App, Packaged (WinUI3 in Desktop) project, and name it MyCppDesktopProject 3.Create a Windows Runtime Component (C++/WinRT) project, and name it ThermometerWRC

 // Thermometer.cpp
#include "pch.h"
#include "Thermometer.h"
#include "Thermometer.g.cpp"

namespace winrt::ThermometerWRC::implementation
{
    void Thermometer::AdjustTemperature(float deltaFahrenheit)
    {
        m_temperatureFahrenheit += deltaFahrenheit;
    }
}

4.Right click on the MyCppDesktopProject project node and select Add->Reference 5.Click the checkbox for ThermometerWRC and click OK. Your MyCppDesktopProject.vcxproj should have a reference to the ThermometerWRC.vcxproj file. 6.Right click on the ThermometerWRC project node and select Add-> New Item 7.Select User Control (WinUI 3) and press OK. 8.Build the solution. It won't successfully build but that's fine. 9.Replace the existing my_ButtonClick method in UserControl.xaml.cpp with the following

  void UserControl::myButton_Click(IInspectable const&, RoutedEventArgs const&)
    {
        myButton().Content(box_value(L"Clicked"));
        ThermometerWRC::Thermometer t;
        t.AdjustTemperature(20);
    }

10.In the pch.h file under the MyCppDesktopProject Node, add the following line: #include <winrt/ThermometerWRC.h> 11.Build and Run the Solution (F5)

Expected behavior

No errors.

Screenshots

image

NuGet package version

WinUI 3 - Windows App SDK 1.4.1: 1.4.230913002

Windows version

Windows 11 (22H2): Build 22621, Windows 10 (21H2): Build 19044

Additional context

No response

AnnYang01 commented 1 year ago

Attach my repro project: \ddfiles3\Team_XAML\User\v-annyang\ThermometerWRC.zip

codendone commented 1 year ago

This might be caused by an old version of the Microsoft.Windows.CppWinRT package. If your project is using a version from 2022 ("2.0.22*"), I think updating to a more recent version should fix this.

ranjeshj commented 11 months ago

@annyang01 Please reactivate if this still repros with latest version. Thanks!

AnnYang01 commented 11 months ago

@ranjeshj @codendone Only C2079 error still exist in Error List after updating the Microsoft.Windows.CppWinRT package to the latest version-2.0.230706.1, I don't have the permission to reactive this issue, please help me reactive it if necessary. image C2079 'winrt::impl::producer::vtable' uses undefined struct 'winrt::impl::produce' with [ D=winrt::ThermometerWRC::implementation::UserControl ]

codendone commented 11 months ago

This remaining error sounds like you're missing a #include <winrt/xxx.h> for the relevant namespace header for that type.

codendone commented 10 months ago

Hopefully adding a missing #include <winrt/xxx.h> fixed your remaining issue. Re-closing this.

AnnYang01 commented 9 months ago

@codendone Could you follow the repro steps and tell me where to insert the code to avoid C2079 error? In the pch.h file under the MyCppDesktopProject Node, we have added the following line:#include <winrt/ThermometerWRC.h>, but C2079 error still exist. image

XamlTest commented 8 months ago

@codendone We still failed with same errors on VS 17.9 Preview 3 with the original steps.

image

After updating all packages to the latest version, errors still exist. image

image

Scottj1s commented 8 months ago

@AnnYang01, the direct cause of the build error is that UserControl.xaml.cpp is missing this: #include <winrt/Microsoft.UI.Xaml.Markup.h> You can add that to either UserControl.xaml.cpp or pch.h to resolve the last build error.

However, you'll then have an issue with mismatched target platforms, since your app is Desktop Windows and your ThermometerWRC is Universal Windows. That can be worked around by other means, but ultimately the best thing is to create your WRC project with the "Windows Runtime Component (WinUI3)" template, and not the "Windows Runtime Component (C++/WinRT)" template. That would have avoided all of the preceding build errors and subsequent runtime errors.

AnnYang01 commented 8 months ago

@Scottj1s Thank you for your response and providing us with a workaround, but unfortunately it did not get rid of the C2079 error for me, please see the screenshot. image We take some time to investigate issue and want to locate which step is causing this issue. thankfully, we can repro this issue using simplified steps, please see Bug 1942026: [Main] Windows Runtime Component (C++/WinRT) failed to build after adding a User Control (WinUI 3) item Not sure if it belongs to WinUI issue or VS. If you have any questions or suggestions, you can add a comment under this bug.