How to link WebView2 statically to a native C++ application using CMake

The Microsoft.Web.WebView2 component is distributed through the Nuget package repository (https://www.nuget.org). 

How to link it to a native C++ project that uses CMake and is compiled using Visual Studio?

First way

You can run the nuget command from CMakeLists.txt, install packages in some directory and include .targets files.

find_program(NUGET_EXE NAMES nuget)
if(NOT NUGET_EXE)
	message("NUGET.EXE not found.")
	message(FATAL_ERROR "Please install this executable, and run CMake again.")
endif()
execute_process(COMMAND ${NUGET_EXE} install "Microsoft.Web.WebView2" -Version 1.0.2592.51 -ExcludeVersion -OutputDirectory ${CMAKE_BINARY_DIR}/packages)
execute_process(COMMAND ${NUGET_EXE} install "Microsoft.Windows.ImplementationLibrary" -Version 1.0.240122.1 -ExcludeVersion -OutputDirectory ${CMAKE_BINARY_DIR}/packages) 
set_target_properties(app PROPERTIES VS_GLOBAL_WebView2LoaderPreference "Static")
target_link_libraries(app PUBLIC ${CMAKE_BINARY_DIR}/packages/Microsoft.Web.WebView2/build/native/Microsoft.Web.WebView2.targets)
target_link_libraries(app PUBLIC ${CMAKE_BINARY_DIR}/packages/Microsoft.Windows.ImplementationLibrary/build/native/Microsoft.Windows.ImplementationLibrary.targets)

However, no matter how hard I tried, the dynamic library was linked. You can, of course, go further and specify the paths to the .lib files, taking into account the target architecture, but there was a better way.

Second way

You need to create the following YourApp.props file in the project root:

<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ImportGroup Label="PropertySheets">     
        <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> 
    </ImportGroup> 
    <ItemGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
        <ProjectCapability Include="PackageReferences" />
    </ItemGroup>

    <PropertyGroup Condition="'$(MSBuildProjectExtension)' == '.vcxproj'">
        <NuGetTargetMoniker Condition="'$(NuGetTargetMoniker)' == ''">native,Version=v0.0</NuGetTargetMoniker>
        <RuntimeIdentifiers Condition="'$(RuntimeIdentifiers)' == ''">win;win-x86;win-x64;win-arm;win-arm64</RuntimeIdentifiers>
    </PropertyGroup>
</Project>

This file will allow VS to work with PackageReference in vcxproj projects.

And add these lines to your CMakeLists.txt:

set_target_properties(app PROPERTIES VS_GLOBAL_WebView2LoaderPreference "Static")
set_target_properties(app PROPERTIES VS_USER_PROPS "${CMAKE_SOURCE_DIR}/YourApp.props")
set_target_properties(app PROPERTIES VS_PACKAGE_REFERENCES "Microsoft.Web.WebView2_1.0.2592.51;Microsoft.Windows.ImplementationLibrary_1.0.240122.1")

Tested on CMake 3.29.5 and Microsoft Visual Studio Community 2019 Version 16.11.36


Comments
Write your comment
Nickname:
Nickname*:
There are no comments yet.