Sciter入门之编译exe分发(二)

上一篇说明了入门:https://lingkang.top/archives/sciter-ru-men-zhi-hello-yi

本章来介绍如何编译exe分发,有多种方案。

前言

Sciter 是一个高质量但小众的嵌入式 UI 引擎,适合追求性能、体积和原生集成的桌面应用开发者。

我觉得 Sciter 比较有意思,它很小众,商业使用需要购买许可,还不算贵。它是Andrew Fedoniouk开发维护,Andrew获得了物理学和应用数学硕士学位以及艺术文凭。他的职业生涯始于俄罗斯航空航天工业的研究员。这种跨领域背景使他既具备深厚的技术功底,又懂得用户界面设计的艺术。

Sciter官网:https://sciter.com/

本次开发环境:window 10 + Clion 2024.3 + Sciter-js v6.0.2.28(2025-11-15最新版) + Bundled MinGW 11.0

设置编译

修改 CMakeLists.txt 在最后面添加

# 启用静态链接(便于分发)
set(CMAKE_EXE_LINKER_FLAGS "-static")
# 启用 Windows GUI 子系统
target_link_libraries(hello_sciter20251115 PRIVATE "-mwindows")

整体配置文件如下

cmake_minimum_required(VERSION 3.30)
project(hello_sciter20251115)

set(CMAKE_CXX_STANDARD 17)

add_executable(hello_sciter20251115 main.cpp)

# sciter头文件所在目录
include_directories("C:\\Users\\Administrator\\Desktop\\project\\sciter\\sciter-js-sdk-main\\include")


# 自动复制 sciter.dll 到exe输出目录(保证运行时不会缺少dll)
add_custom_command(TARGET hello_sciter20251115 POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
        "C:\\Users\\Administrator\\Desktop\\project\\sciter\\sciter-js-sdk-main\\bin\\windows\\x64\\sciter.dll"
        $<TARGET_FILE_DIR:hello_sciter20251115>
)

# 启用静态链接(可选,便于分发)
set(CMAKE_EXE_LINKER_FLAGS "-static")
# 启用 Windows GUI 子系统
target_link_libraries(hello_sciter20251115 PRIVATE "-mwindows")

方案一:相对exe路径(简单)

修改main.cpp代码

int uimain(std::function<int()> run) {
    // 创建ui窗口实例
    sciter::om::hasset<myWindow> window = new myWindow();

    // 加载前端UI的html文件
    // window->load(WSTR("file://C:/Users/Administrator/Desktop/project/sciter/app/hello_sciter20251115/ui/hello-ui.html"));

    // 方案一:使用当面exe所在目录的  ui/hello-ui.html
    bool res= window->load(WSTR("file://ui/hello-ui.html"));
    std::cout << " 加载html情况:" << res << std::endl;

    window->expand();
    return run();
}

打包:Clion编辑CMaKe配置

image-1763214620820

切换到Release即可
image-1763214660410

选择编译:Rebuild All in 'Release'
image-1763214702110

创建一个目录

  • hello_sciter20251115.exe
  • ui (源码的ui目录)
  • sciter.dll (位于sciter-js-sdk-main\bin\windows.d2d\x64\sciter.dll ,最小分发原则)

将上面文件放到同一个目录
image-1763214885707

运行 hello_sciter20251115.exe 或者找个虚拟机、发给你的好基友运行它:

image-1763215015775

  • 这是最简单的方案,容易更新ui
  • 缺点就是别人可以编辑的ui源码

方案二:试用packfolder.exe打包前端资源到二进制

此方案会把前端源码打包到 exe 中一定程度防止修改源码

编写一个打包脚本 pack-resources.bat

C:\Users\Administrator\Desktop\project\sciter\sciter-js-sdk-main\bin\windows\packfolder.exe ui resources.cpp -v "resources"

运行它将会得到resources.cpp

修改 main.cpp 源码

#include "resources.cpp" // 引入打包的资源

int uimain(std::function<int()> run) {
    // 绑定前端源码
    sciter::archive::instance().open(aux::elements_of(resources));

    // 创建ui窗口实例
    sciter::om::hasset<myWindow> window = new myWindow();

    // 加载前端UI的html文件
    // window->load(WSTR("file://C:/Users/Administrator/Desktop/project/sciter/app/hello_sciter20251115/ui/hello-ui.html"));

    // 方案一:使用当面exe所在目录的 ui/hello-ui.html
//    bool res= window->load(WSTR("file://ui/hello-ui.html"));
//    std::cout << " 加载html情况:" << res << std::endl;

    // 方案二:使用打包资源 resources.cpp,开头必须是 this://app,不用再加 ui 目录,直接使用里面的 hello-ui.html 前端文件名称
    window->load(WSTR("this://app/hello-ui.html"));

    window->expand();
    return run();
}

改造截图如下

image-1763216506437

编译
image-1763216544174

  • hello_sciter20251115.exe
  • sciter.dll (位于sciter-js-sdk-main\bin\windows.d2d\x64\sciter.dll ,最小分发原则)

运行它(其他Windows7+系统)

image-1763216860155

  • 该方法增加破解、修改难度(一点点)
  • 如果你购买它许可,使用静态链接库可以打包成一个exe,无 sciter.dll