microsoft / cppwinrt

C++/WinRT
MIT License
1.65k stars 238 forks source link

Is it possible to add a new idl with auto-generated .h and .cpp file associated with the idl file #637

Closed petercoin closed 4 years ago

petercoin commented 4 years ago

Hi, I tried to add a new idl file but I found that the .h and .cpp file will not be generated automatically. After adding the .h and .cpp files manually, they were not associated with the idl file like the Class.idl, Class.h and Class.h in the C++/WinRT project template. I had to edit .vcxproj file to add the DependentUpon property for .h and .cpp file manually and then they were associated with the idl file. Is there a way to do this automatically?

jlaanstra commented 4 years ago

This was a bug in Visual Studio 2019 Update 5 and should be fixed in Update 6.

mk2 commented 4 years ago

@jlaanstra Hi, I have installed the Visual Studio 16.6, but have been faced the same problem. What is Update 6?

Gavin-Williams commented 3 years ago

I'm not sure if I'm understanding the issue here, but h and cpp files are not automatically associated with their corresponding IDL files - this has been the case any time I have tried C++/WinRT, which would be back in 2020, and now in 2021.

UWP Community Discord was very helpful in explaining that the way I get correct file association is by utilizing the C++ item template:

ViewModel (C++/WinRT) An empty interface definition suitable for XAML data binding, for a C++/WinRT UWP app.

Note that I would never in a million years think that this is the right item to use. I'm not using Xaml, I'm not doing data-binding, I'm not using a UI framework or a ViewModel pattern. How am I meant to read that and think 'that's what I use to make a WinRT Component class?

And back to the main issue, neither allowing the source generator to create the H & CPP files, nor manually adding source files give the correct file association. That ViewModel template did.

That means the documentation on creating runtime components is incorrect. And the obvious way to make a runtime component classes (manually creating IDL, H & CPP files) doesn't correctly configure the file association in the project file. I don't care for the technique of using build to generate the source files - you can throw that whole idea away as far as I'm concerned.

So this whole procedure is hot garbage I reckon:

You can do a lot better than that.

Firstly, when IDL, H or CPP files are added to a project, the solution explorer needs to pick up immediately on association by name. So for example, when Model.h is added to a project which already contains Model.idl, the project file is automatically updated (with \ or whatever) to create the association in solution explorer which results in h and cpp files being nested under the idl.

And secondly, you also need an item template, correctly categorized may I add.. C++ / Code / C++/WinRT Component Class.

Edit: I may post a new issue for this.