pu369com

Rust 中编译出的可执行文件默认会显示控制台窗口(黑框),如何隐藏黑框

    1. ‌修改编译目标为 Windows 子系统 (GUI)‌

      • 在 Cargo.toml 中添加配置指定目标为 Windows GUI 应用:
        tomlCopy Code
         
        [package.metadata.windows_subsystem] windows = "windows"
        或直接通过 Rust 编译器参数指定:
        bashCopy Code
         
        rustc --crate-type bin --target x86_64-pc-windows-msvc -C link-args="/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup" src/main.rs ```‌:ml-citation{ref="1,2" data="citationList"}
    2. ‌使用 #![windows_subsystem = "windows"] 属性‌

      • 在 Rust 源码文件顶部添加以下属性声明:
        rustCopy Code
         
        #![windows_subsystem = "windows"]
        这会强制编译器生成无控制台窗口的 GUI 应用‌2。
    3. ‌通过链接器参数配置‌

      • 若使用 cargo 构建,可在 .cargo/config.toml 中配置链接器参数:
        tomlCopy Code
         
        [target.x86_64-pc-windows-msvc] rustflags = ["-C", "link-args=/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup"] ```‌:ml-citation{ref="1" data="citationList"}

    对于需要调用外部程序(如通过 std::process::Command)的情况,建议额外设置进程创建标志为 CREATE_NO_WINDOW(需调用 Windows API)以避免子进程弹出黑框‌3。若需进一步优化打包流程,可结合 PyInstaller 的 --noconsole 参数思路,通过构建脚本调整最终二进制文件的属性‌

     

posted on 2025-07-18 18:44  pu369com  阅读(215)  评论(0)    收藏  举报

导航