Answered by:
[UWP][C++] UWP Hello World Console App with CMake

Question
-
I am trying to build a UWP console "hello, world" program using CMake, and not the app wizard in Visual Studio 2017, or the Visual Studio IDE. So far, I have been able to successfully create a .sln, and compile the program, but when it runs, a dialog pops up with "Unable to activate Windows Store app ... The sandbox.exe process started, but the activation request failed with error 'The app didn't start'." if I run from within Visual Studio.
If I try to run from the command line, I get a "The code execution cannot proceed because VCRUNTIME140D_APP.dll was not found." system error.
The CMakeLists.txt file looks like this:
cmake_minimum_required(VERSION 3.12) project(sandbox VERSION 0.0.1 LANGUAGES CXX) set(sandbox_source main.c) set_source_files_properties(${sandbox_source} PROPERTIES LANGUAGE CXX) add_executable(sandbox ${sandbox_source}) set_property(TARGET sandbox PROPERTY CXX_STANDARD 17) set_property(TARGET sandbox PROPERTY VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION 10.0.17134.0)
And the source file looks like this:
#include "winrt/Windows.ApplicationModel.Core.h" #include <stdio.h> int main() { printf("Hello, world.\n"); return 0; }
Are there any other required files? The Visual Studio wizard creates a Package.appxmanifest and a .pfx cert when you create a WinRt console application, but I am not sure how to reference those in CMake, or even if I need to.
- Moved by Roy LiMicrosoft contingent staff Wednesday, April 17, 2019 8:48 AM off-topic
Tuesday, April 16, 2019 8:33 PM
Answers
-
After much trial and error and combing the internet, here is what I have found you need to get a minimal hello world console app with cmake:
- Three image files in an "Assets" directory under your project directroy -- logo-44x44.png, logo-150x150.png, and a 50x50 StoreLogo.png.
- A TemporaryKey.pfx certificate. I "borrowed" one from a WinRT console app Visual Studio created.
- A Package.appxmanifest file. This is the application configuration. An example is below.
- main.cpp. The hello world source. See below.
- CMakeLists.txt. See below.
Package.appxmanifest looks like this:
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" xmlns:iot2="http://schemas.microsoft.com/appx/manifest/iot/windows10/2" IgnorableNamespaces="uap mp uap5 iot2 desktop4"> <Identity Name="sandbox-uwp" Publisher="CN=YourNameHere" Version="1.0.0.0" /> <mp:PhoneIdentity PhoneProductId="00000000-0000-0000-0000-000000000000" PhonePublisherId="00000000-0000-0000-0000-000000000000"/> <Properties> <DisplayName>sandbox-uwp</DisplayName> <PublisherDisplayName>YourNameHere</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" /> </Dependencies> <Resources> <Resource Language="x-generate"/> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="sandbox-uwp.App" desktop4:Subsystem="console" desktop4:SupportsMultipleInstances="true" iot2:Subsystem="console" iot2:SupportsMultipleInstances="true" > <uap:VisualElements DisplayName="sandbox-uwp" Square150x150Logo="Assets\logo-150x150.png" Square44x44Logo="Assets\logo-44x44.png" Description="whatever" BackgroundColor="transparent"> </uap:VisualElements> <Extensions> <uap5:Extension Category="windows.appExecutionAlias" Executable="sandbox-uwp.exe" EntryPoint="sandbox-uwp.App"> <uap5:AppExecutionAlias desktop4:Subsystem="console" iot2:Subsystem="console"> <uap5:ExecutionAlias Alias="sandbox-uwp.exe" /> </uap5:AppExecutionAlias> </uap5:Extension> </Extensions> </Application> </Applications> </Package>
main.cpp looks like this:
#include "winrt/Windows.ApplicationModel.Core.h" #include <stdio> int main() { printf("Hello, world.\n"); return 0; }
And the all-important CMakeLists.txt looks like this:
# # Minimal configuration for building a UWP-Console app. # cmake_minimum_required(VERSION 3.12) # Configure CMake for UWP. You can do this on the command line with # -D<variable>=<value>, which may be better for cross-platform builds, but gets # tedious when you are only building for a single platform. If you are doing # it this way, in the CMAKE file, you have to set it before the project # declaration! set(CMAKE_SYSTEM_NAME "WindowsStore") set(CMAKE_SYSTEM_VERSION "10.0") project(sandbox VERSION 0.0.1 LANGUAGES CXX) # Target files. set(source main.cpp) set( assets Assets/logo-44x44.png Assets/logo-150x150.png Assets/StoreLogo.png ) set(resources ${assets} TemporaryKey.pfx Package.appxmanifest) # Assets have to be marked as deployment content, and given a location, so they # will be packaged into the built app. set_source_files_properties( ${assets} PROPERTIES VS_DEPLOYMENT_CONTENT 1 ) set_source_files_properties( ${assets} PROPERTIES VS_DEPLOYMENT_LOCATION "Assets" ) # Target application settings. add_executable(sandbox-uwp ${source} ${resources}) set_property(TARGET sandbox-uwp PROPERTY CXX_STANDARD 17) set_property( TARGET sandbox-uwp PROPERTY VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION 10.0.17134.0 )
With that, you should be able to create a Visual Studio solution with `cmake <path to project>`, and build from the command line with `cmake --build .`.
Once the application is built, you need to install it. Do this from Power Shell.
PS > Add-AppxPackage
And follow the prompts.
Once the application is installed, you should be able to run it from the command line, or from the start menu.
PS > sandbox-uwp Hello world.
To remove the application, use Get-AppxPackage to find the PackageFullName of the application.
PS > Get-AppxPackage -Name sandbox-uwp* Name : sandbox-uwp ... PackageFullName : sandbox-uwp_1.0.0.0_x86__xxxxxxxxxxxxx ...
Then remove the application with Remove-AppxPackage.
PS > Remove-AppxPackage -Package sandbox-uwp_1.0.0.0_x86__xxxxxxxxxxxxx
I hope I transcribed everything correctly, and that this saves someone some time.
- Marked as answer by andrew_was_here Tuesday, June 11, 2019 4:24 PM
Tuesday, June 11, 2019 4:24 PM
All replies
-
Hi,
I'm sorry to say that this problem with CMake is out of the scope of UWP forum, I suggest that you might need to ask about this in CMake's official forum or contact their technical support.
Best regards,
Roy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.Wednesday, April 17, 2019 2:39 AM -
After much trial and error and combing the internet, here is what I have found you need to get a minimal hello world console app with cmake:
- Three image files in an "Assets" directory under your project directroy -- logo-44x44.png, logo-150x150.png, and a 50x50 StoreLogo.png.
- A TemporaryKey.pfx certificate. I "borrowed" one from a WinRT console app Visual Studio created.
- A Package.appxmanifest file. This is the application configuration. An example is below.
- main.cpp. The hello world source. See below.
- CMakeLists.txt. See below.
Package.appxmanifest looks like this:
<?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5" xmlns:desktop4="http://schemas.microsoft.com/appx/manifest/desktop/windows10/4" xmlns:iot2="http://schemas.microsoft.com/appx/manifest/iot/windows10/2" IgnorableNamespaces="uap mp uap5 iot2 desktop4"> <Identity Name="sandbox-uwp" Publisher="CN=YourNameHere" Version="1.0.0.0" /> <mp:PhoneIdentity PhoneProductId="00000000-0000-0000-0000-000000000000" PhonePublisherId="00000000-0000-0000-0000-000000000000"/> <Properties> <DisplayName>sandbox-uwp</DisplayName> <PublisherDisplayName>YourNameHere</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" /> </Dependencies> <Resources> <Resource Language="x-generate"/> </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="sandbox-uwp.App" desktop4:Subsystem="console" desktop4:SupportsMultipleInstances="true" iot2:Subsystem="console" iot2:SupportsMultipleInstances="true" > <uap:VisualElements DisplayName="sandbox-uwp" Square150x150Logo="Assets\logo-150x150.png" Square44x44Logo="Assets\logo-44x44.png" Description="whatever" BackgroundColor="transparent"> </uap:VisualElements> <Extensions> <uap5:Extension Category="windows.appExecutionAlias" Executable="sandbox-uwp.exe" EntryPoint="sandbox-uwp.App"> <uap5:AppExecutionAlias desktop4:Subsystem="console" iot2:Subsystem="console"> <uap5:ExecutionAlias Alias="sandbox-uwp.exe" /> </uap5:AppExecutionAlias> </uap5:Extension> </Extensions> </Application> </Applications> </Package>
main.cpp looks like this:
#include "winrt/Windows.ApplicationModel.Core.h" #include <stdio> int main() { printf("Hello, world.\n"); return 0; }
And the all-important CMakeLists.txt looks like this:
# # Minimal configuration for building a UWP-Console app. # cmake_minimum_required(VERSION 3.12) # Configure CMake for UWP. You can do this on the command line with # -D<variable>=<value>, which may be better for cross-platform builds, but gets # tedious when you are only building for a single platform. If you are doing # it this way, in the CMAKE file, you have to set it before the project # declaration! set(CMAKE_SYSTEM_NAME "WindowsStore") set(CMAKE_SYSTEM_VERSION "10.0") project(sandbox VERSION 0.0.1 LANGUAGES CXX) # Target files. set(source main.cpp) set( assets Assets/logo-44x44.png Assets/logo-150x150.png Assets/StoreLogo.png ) set(resources ${assets} TemporaryKey.pfx Package.appxmanifest) # Assets have to be marked as deployment content, and given a location, so they # will be packaged into the built app. set_source_files_properties( ${assets} PROPERTIES VS_DEPLOYMENT_CONTENT 1 ) set_source_files_properties( ${assets} PROPERTIES VS_DEPLOYMENT_LOCATION "Assets" ) # Target application settings. add_executable(sandbox-uwp ${source} ${resources}) set_property(TARGET sandbox-uwp PROPERTY CXX_STANDARD 17) set_property( TARGET sandbox-uwp PROPERTY VS_WINDOWS_TARGET_PLATFORM_MIN_VERSION 10.0.17134.0 )
With that, you should be able to create a Visual Studio solution with `cmake <path to project>`, and build from the command line with `cmake --build .`.
Once the application is built, you need to install it. Do this from Power Shell.
PS > Add-AppxPackage
And follow the prompts.
Once the application is installed, you should be able to run it from the command line, or from the start menu.
PS > sandbox-uwp Hello world.
To remove the application, use Get-AppxPackage to find the PackageFullName of the application.
PS > Get-AppxPackage -Name sandbox-uwp* Name : sandbox-uwp ... PackageFullName : sandbox-uwp_1.0.0.0_x86__xxxxxxxxxxxxx ...
Then remove the application with Remove-AppxPackage.
PS > Remove-AppxPackage -Package sandbox-uwp_1.0.0.0_x86__xxxxxxxxxxxxx
I hope I transcribed everything correctly, and that this saves someone some time.
- Marked as answer by andrew_was_here Tuesday, June 11, 2019 4:24 PM
Tuesday, June 11, 2019 4:24 PM