GKLBB

当你经历了暴风雨,你也就成为了暴风雨

导航

软件研发 --- 如何开发不依赖net的exe

如果你的 Windows 系统没有安装 .NET Framework 或 .NET Core/5+/6+,你仍然可以通过以下方法编译生成可执行的 .exe 文件(不依赖 .NET 运行时):


1. 使用原生 Win32 API(C/C++)

适用语言:C、C++
工具链:MinGW、MSVC(Visual Studio 编译器)、Clang
特点:生成纯原生代码,无额外依赖

方法 1:MinGW(GCC for Windows)

  1. 安装 MinGW

    • 下载 MinGW-w64 或 TDM-GCC

    • 安装后确保 gcc 可用(如 gcc --version 测试)

  2. 编译 C 代码

    c
     
    // hello.c
    #include <stdio.h>
    int main() {
        printf("Hello, World!\n");
        return 0;
    }

    编译命令:

    sh
     
    gcc hello.c -o hello.exe
    • 生成的 hello.exe 可直接运行(无需 .NET)。


方法 2:MSVC(Visual Studio 编译器)

  1. 安装 Visual Studio Build Tools(无需完整 IDE)

  2. 编译 C/C++ 代码

    sh
     
    cl hello.c /Fe:hello.exe
    • 生成的 .exe 是原生 Win32 程序。


2. 使用 Go 语言

适用语言:Go
特点:静态编译,无外部依赖

  1. 安装 Go

  2. 编译 Go 代码

    go
     
    // hello.go
    package main
    import "fmt"
    func main() {
        fmt.Println("Hello, World!")
    }

    编译命令:

    sh
     
    go build hello.go
    • 生成 hello.exe,无需 Go 运行时即可运行。


3. 使用 Rust

适用语言:Rust
特点:内存安全,生成原生代码

  1. 安装 Rust

    sh
     
    curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

    (或下载 Rust for Windows

  2. 编译 Rust 代码

    rust
     
    // main.rs
    fn main() {
        println!("Hello, World!");
    }

    编译命令:

    sh
     
    rustc main.rs
    • 生成 main.exe,无需额外运行时。


4. 使用 Free Pascal(Delphi 替代方案)

适用语言:Pascal
特点:轻量级,生成原生代码

  1. 安装 Free Pascal

  2. 编译 Pascal 代码

    pascal
     
    // hello.pas
    program Hello;
    begin
        Writeln('Hello, World!');
    end.

    编译命令:

    sh
     
    fpc hello.pas
    • 生成 hello.exe,无依赖。


5. 使用 V(Vlang)

适用语言:V
特点:极简语法,快速编译

  1. 安装 V

  2. 编译 V 代码

    v
     
    // hello.v
    fn main() {
        println("Hello, World!")
    }

    编译命令:

    sh
     
    v hello.v
    • 生成 hello.exe,无外部依赖。


6. 使用 Batch 转 EXE(仅简单脚本)

适用语言:Batch(.bat
工具:Bat To Exe Converter

  1. 下载工具

  2. 转换 .bat 到 .exe

    • 打开 .bat 文件 → 转换为 .exe

    • 生成的 .exe 仍依赖 cmd.exe,但无需 .NET。


7. 使用 Python 转 EXE(需 Python 但无需 .NET)

适用语言:Python
工具:PyInstallerNuitka

  1. 安装 Python

  2. 编译 Python 脚本

    sh
     
    pip install pyinstaller
    pyinstaller --onefile hello.py
    • 生成 dist/hello.exe(包含 Python 运行时,但不依赖 .NET)。


8. 使用 Zig(新兴语言)

适用语言:Zig
特点:轻量级,可交叉编译

  1. 安装 Zig

  2. 编译 Zig 代码

    zig
     
    // hello.zig
    pub fn main() void {
        @import("std").debug.print("Hello, World!\n", .{});
    }

    编译命令:

    sh
     
    zig build-exe hello.zig

posted on 2025-06-29 16:54  GKLBB  阅读(39)  评论(0)    收藏  举报