Publishing a mixed C++/.NET solution all at once
I have a solution with a mix of C++ and C#/.NET 9 projects.
For the .NET part of the solution, I can publish with
dotnet publish {SOLUTION_FILE_NAME} /p:PublishProfile="PROFILE_NAME"
. This invokes a publish profile with the given name for all C# projects in the solution, and works as intended.
However, obviously dotnet publish
cannot deal with the C++ projects in the solution. Is there a way- given all projects have the artifact output path specified- for the entire solution to be published at once using either VS or a Developer PowerShell?25 Replies
does it work if you use
msbuild
insteadDoesn't seem so. Replacing
dotnet
with msbuild
in that invocation gives MSB1008: Only one project can be specified.
- and it seems to call MSBuild {...} for .NET Framework
I'm not sure how exactly to do that properly.msbuild -t:Publish -p:_IsPublishing=true ...
Hm. That does indeed do something more, but all the .NET projects complain that:
but the project does define:
(where the publish profiles all indicate a
win-x86
target)msbuild -restore -t:Publish -p:_IsPublishing=true
Right. That did the job for all the .NET projects. I only pieced together now that VS doesn't let me use its publish profile editor for the C++ projects. How does one make a publish profile for a C++ project?
What ends up happening with no profile for the C++ bits is:
I would have expected both to be 'unpublishable'. No idea why it tried to build the first one.
it doesn't seem like C++ supports PublishProfile
Ouch. Well, I could do with it simply copying the build output where I tell it to for those two specific projects.
I guess another way of doing this is just to force a .NET publish on every build... and then a post-build copy task for those two C++ projects only. I found https://github.com/dotnet/msbuild/issues/3138 which uses:
to force a publish on build, but doesn't seem to take a profile name parameter. I'll look further into it.
i suppose you could add something which reads PublishProfile to your vcxproj files
why are you using publish profiles
I figured they were the way to specify an output directory and RID/platform target all at once.
Since I am compiling specifically for Windows x86 and some dependencies (ImGui C# bindings) have to select the correct native DLL to copy based on the target arch.
i mean. for publish, yes. if you are trying to do publish on build, just to use publish profiles, then you don't want publish profiles
Sure. What should I have gone for then?
Customize your build with extensibility hooks - MSBuild
Customize your build with several extensibility hooks that you can use to modify MSBuild projects that use the standard build process.
Sorry. I'm confused. Maybe I should explain how I got here:
- I have a solution with 12-13 projects, some .NET, some C++
- On build (or explicitly) I'd like to arrange them in output directories of my choice
- I used to define <CopyToDir> in every
.csproj
or .vcxproj
and had a Directory.Build.targets
in the root solution folder like:
- What ended up happening here is that I wouldn't properly copy over everything. I'd end up with either more files than intended (because it wouldn't pick out only the relevant parts for the selected RID) or fewer
- Publishing with a profile with a given RID didn't have this problem (and allowed me to do ex. single-file deployment), but now I'd have to first build C++ projects with this "CopyAfterBuild" target then publish to get everything I need
- I was hoping to condense that down to one action, whichever of the two (building or publishing)you should not be using custom targets to customize the output folder at all
I figured that at some point. So publish profiles were me trying to move away from that.
you are able to set the output directories that the build will use yourself
using these extensibility points
Alright. I'll look into them. Thank you very much for the help and patience.
like, here is an example of using it to change the output directory for all projects in a folder https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory?view=vs-2022#directorybuildprops-example
Okay. And if I specify
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
in a .csproj
that would ensure the build is done for the correct arch?
(or, rather, is that the same as me previously giving that in a publish profile?)yes
publish profile is the same as putting it in a project
Great, thanks a ton. I think I've figured it out now.
š
Alright. I managed to figure it out. One final question. Can single-file deployment for .NET executables be specified for builds as well or is it a publishing-only thing?
it is publish-only
Unfortunate, but I'll make do, at least now the build is all neatly placing things where they belong with
Directory.Build.props
re: Publish Profiles - in VS it's the same as putting it in your project file, but in the CLI today there are subtle differences that we haven't yet papered over. Here's the tracking issue we have for this: https://github.com/dotnet/sdk/issues/11474