Skip to content

Hello World!

This page documents a simple Hello-World project

  • Directorysrc
    • main.cpp
  • CMakeLists.txt

It is recommended to consume saucer through CMake (see Installation).
While it is recommended to use CPM.cmake, the following example will use FetchContent for simplicty.

CMakeLists.txt
cmake_minimum_required(VERSION 3.21)
project(hello-world LANGUAGES CXX VERSION 1.0.0)
# --------------------------------------------------------------------------------------------------------
# Create executable
# --------------------------------------------------------------------------------------------------------
add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE "src/main.cpp")
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_23)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 23 CXX_EXTENSIONS OFF CXX_STANDARD_REQUIRED ON)
# --------------------------------------------------------------------------------------------------------
# Link libraries
# --------------------------------------------------------------------------------------------------------
include(FetchContent)
FetchContent_Declare(
saucer
GIT_REPOSITORY "https://github.com/saucer/saucer"
GIT_TAG v7.0.0
)
FetchContent_MakeAvailable(saucer)
target_link_libraries(${PROJECT_NAME} PRIVATE saucer::saucer)
  • Directorysrc
    • main.cpp
  • CMakeLists.txt

Now that CMake is setup, it is time to write a simple saucer program!

src/main.cpp
#include <print>
#include <saucer/smartview.hpp>
coco::stray start(saucer::application *app)
{
auto window = saucer::window::create(app).value();
auto webview = saucer::smartview<>::create({.window = window});
window->set_title("Hello World!");
webview->expose("call_me", [&](double a, double b)
{
std::println("Called with: a = {}, b = {}", a, b);
return a + b;
});
webview->expose("call_me_too", [&]() -> coco::task<double>
{
auto random = co_await webview->evaluate<double>("Math.random()");
std::println("Random: {}", random);
co_return random;
});
webview->set_url("https://saucer.app");
window->show();
co_await app->finish();
}
int main()
{
return saucer::application::create({.id = "hello-world"})->run(start);
}