webview
repository·master·Indexed 12 days ago
https://github.com/webview/webviewA tiny, cross-platform C/C++ library for building modern GUIs using HTML5, CSS, and JavaScript. It provides a common abstraction layer for platform web engines such as WebKit and WebView2, supporting two-way JavaScript bindings and thread-safe dispatching.
What's inside webview
- webview is a cross-platform library that provides an HTML5 UI abstraction layer. It supports two-way JavaScript bindings, allowing you to call JavaScript from C/C++ and call C/C++ from JavaScript.
Manage Application Lifecycle for existing windows (v0.11.0+)
masterIn
webviewv0.11.0 and later, if you pass an existing window to the library, the library will not perform platform-specific initialization. You must manage the lifecycle yourself.Specifically, the following tasks are now only performed automatically if the library creates the window for you:
Platform Initialization Task Linux Calling gtk_init_check()Windows COM initialization and DPI awareness enablement Handle thread safety in webview
masterLibrary functions generally do not have thread safety guarantees. To interact with the webview from other threads safely, use the following methods to schedule code execution on the main/GUI thread:
- C: Use
webview_dispatch() - C++: Use
webview::dispatch()
Additionally,
webview_return()(C) andwebview::resolve()(C++) are safe to call from other threads as they usedispatchinternally.Note: The main/GUI thread must be the thread that calls
webview_run()(C) orwebview::run()(C++).- C: Use
Non-CMake Usage (Direct Compiler Invocation)
masterIf you are not using CMake, you can invoke compilers directly. Place the amalgamated
webview.h(or individual files) inlibs/webviewand the MS WebView2 header inlibsfor Windows.macOS
c++ main.cc -O2 --std=c++11 -Ilibs -framework WebKit -ldl -o exampleLinux
c++ main.cc -O2 --std=c++11 -Ilibs $(pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.1) -ldl -o exampleWindows
c++ main.cc -O2 --std=c++14 -static -mwindows -Ilibs -ladvapi32 -lole32 -lshell32 -lshlwapi -luser32 -lversion -o exampleCross-compile webview for Windows x64 on Linux
masterYou can cross-compile using the toolchain files in the
cmake/toolchainsdirectory. To target Windows x64 on Linux with POSIX threads, use the following command:cmake -G "Ninja Multi-Config" -B build -S . -D CMAKE_TOOLCHAIN_FILE=cmake/toolchains/x86_64-w64-mingw32.cmake -D WEBVIEW_TOOLCHAIN_EXECUTABLE_SUFFIX=-posix cmake --build build --config CONFIGcmake -G "Ninja Multi-Config" -B build -S . -D CMAKE_TOOLCHAIN_FILE=cmake/toolchains/x86_64-w64-mingw32.cmake -D WEBVIEW_TOOLCHAIN_EXECUTABLE_SUFFIX=-posix cmake --build build --config ReleaseGenerate language bindings using SWIG
masterYou can generate language bindings for the
webviewlibrary using the provided SWIG interface filewebview.i. This allows you to use the library in languages like Python, C#, Java, or Ruby.To generate bindings, use the
swigcommand with the-c++flag and specify the target language and output directory. The following examples use Unix-style command lines.# Create directories for the bindings mkdir -p build/bindings/{python,csharp,java,ruby} # Generate Python bindings swig -c++ -python -outdir build/bindings/python -o build/bindings/python/python_wrap.cpp webview.i # Generate C# bindings swig -c++ -csharp -outdir build/bindings/csharp -o build/bindings/csharp/csharp_wrap.cpp webview.i # Generate Java bindings swig -c++ -java -outdir build/bindings/java -o build/bindings/java/java_wrap.cpp webview.i # Generate Ruby bindings swig -c++ -ruby -outdir build/bindings/ruby -o build/bindings/ruby/ruby_wrap.cpp webview.iBuild webview from source
masterTo build the project using CMake and Ninja:
- Configure the build directory:
cmake -G "Ninja Multi-Config" -B build -S . - Build the project (replace
CONFIGwithDebug,Release, orProfile):cmake --build build --config CONFIG
To run tests:
ctest --test-dir build --build-config CONFIGcmake -G "Ninja Multi-Config" -B build -S . cmake --build build --config Release- Configure the build directory:
Build the Amalgamated Library
masterYou can create an amalgamated library using the
amalgamate.pyscript. This is useful for distributing the library as a single header.python3 scripts/amalgamate/amalgamate.py --base core --search include --output webview_amalgamation.h srcInstall dependencies on Windows
masterFor Windows development:
- Your compiler must support C++14.
- It is recommended to use Visual Studio 2022 or later.
- Developers and end-users must have the WebView2 runtime installed (for Windows versions prior to Windows 11).
- You can obtain WebView2 from NuGet.
- Required Windows libraries:
advapi32,ole32,shell32,shlwapi,user32,version.
Prerequisites for webview
masterBefore using webview, ensure your compiler supports at least C++11 (or C++14 for Windows). While CMake and Ninja are recommended, they are not strictly required.Install dependencies on Linux and BSD
masterLinux and BSD require GTK and WebKitGTK libraries. You must use
pkg-configto retrieve the correct compiler and linker flags.Debian Packages
- WebKitGTK 6.0, GTK 4:
- Development:
apt install libgtk-4-dev libwebkitgtk-6.0-dev - Production:
apt install libgtk-4-1 libwebkitgtk-6.0-4
- Development:
- WebKitGTK 4.1, GTK 3, libsoup 3:
- Development:
apt install libgtk-3-dev libwebkit2gtk-4.1-dev - Production:
apt install libgtk-3-0 libwebkit2gtk-4.1-0
- Development:
- WebKitGTK 4.0, GTK 3, libsoup 2:
- Development:
apt install libgtk-3-dev libwebkit2gtk-4.0-dev - Production:
apt install libgtk-3-0 libwebkit2gtk-4.0-37
- Development:
Fedora Packages
- WebKitGTK 6.0, GTK 4:
- Development:
dnf install gtk4-devel webkitgtk6.0-devel - Production:
dnf install gtk4 webkitgtk6.0
- Development:
- WebKitGTK 4.1, GTK 3, libsoup 3:
- Development:
dnf install gtk3-devel webkit2gtk4.1-devel - Production:
dnf install gtk3 webkit2gtk4.1
- Development:
- WebKitGTK 4.0, GTK 3, libsoup 2:
- Development:
dnf install gtk3-devel webkit2gtk4.0-devel - Production:
dnf install gtk3 webkit2gtk4.0
- Development:
FreeBSD Packages
- GTK 4:
pkg install webkit2-gtk4 - GTK 3:
pkg install webkit2-gtk3
Linking Requirements
- Linux: Use
pkg-configwith--cflagsand--libsfor one of these sets:gtk4 webkitgtk-6.0,gtk+-3.0 webkit2gtk-4.1, orgtk+-3.0 webkit2gtk-4.0. Also linkdl. - macOS: Link frameworks
WebKitand librarydl. - BSD: Execution may require adding the
wxallowedoption to yourfstabto bypass W^X memory protection.
- WebKitGTK 6.0, GTK 4:
Migrate CMake targets from v0.11.0 to v0.12.0
masterWhen upgrading from
webviewv0.11.0 to v0.12.0, several CMake target names have changed to align with exported/installed target names. Update yourCMakeLists.txtto use the following replacements:Old target Replacement webview::headerswebview::corewebview::sharedwebview::core_sharedwebview::staticwebview::core_static# Example replacement target_link_libraries(my_app PRIVATE webview::core)