You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust中SDL2 crate的gfx功能使用:获取SDL2_gfx.lib遇阻求助

问题

我正在用SDL2 crate开发简单物理模拟引擎,想启用gfx功能来获取帧率、绘制图元。我知道得把SDL2_gfx.lib放到路径C:\Users\{Your Username}\.rustup\toolchains\{current toolchain}\lib\rustlib\{current toolchain}\lib里,就像安装SDL2.lib那样,但找不到SDL2_gfx.lib文件。

项目仓库指向了SDL2_gfx的下载源,但下载的文件夹里完全没.lib文件。我试过用VS Code编译SDL2_gfx的解决方案生成.lib,但因为缺少头文件失败,我不熟悉C/C++和VS Code,搞不定这个问题。请问怎么获取SDL2_gfx.lib和对应的.dll文件?

当前代码

use sdl2::{pixels::Color, event::Event, keyboard::Keycode, rect::{Rect, Point}}; 
use sdl2::gfx::*;

const WIDTH: u32 = 1000; 
const HEIGHT: u32 = 700;

const RECT_SIZE: u32 = 30;

pub fn main() {
    let sdl_context = sdl2::init().unwrap();
    let video_subsystem = sdl_context.video().unwrap();

    let window = video_subsystem.window("Physics Sim", WIDTH, HEIGHT)
        .position_centered()
        .build()
        .unwrap();

    let width = window.size().0;
    let height = window.size().1;

    let mut canvas = window.into_canvas().build().unwrap();

    canvas.set_draw_color(Color::RGB(0, 0, 0));
    canvas.clear();
    canvas.present();
    let mut event_pump = sdl_context.event_pump().unwrap();
    let mut i = 0;

    let mut rect = Rect::from_center(Point::new(width as i32 / 4, height as i32 / 4), RECT_SIZE, RECT_SIZE);
    let rect_border_width = 1;

    'running: loop {
        i = (i + 1) % 255;

        canvas.set_draw_color(Color::RGB(0, 0, 0));
        canvas.clear();

        // Inside of rect
        canvas.set_draw_color(Color::RGB(i, 64, 255 - i));
        canvas.fill_rect(rect).unwrap();
        
        // Border of rect
        canvas.set_draw_color(Color::RGB(255, 255, 255));
        canvas.draw_rect(rect).unwrap();

        for event in event_pump.poll_iter() {
            match event {
                Event::Quit {..} |
                Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
                    break 'running
                },
                _ => {}
            }
        }
        // game loop
        let rect_y = rect.y();
        if rect_y + rect_border_width + RECT_SIZE as i32 <= height as i32 {
            rect.set_y(rect_y);
        }

        canvas.present();
        ::std::thread::sleep(std::time::Duration::new(0, 1_000_000_000u32 / 60));
    } 
}

Cargo.toml配置

[dependencies]
sdl2 = {version = "0.35.2", default-features = false, features = ["gfx"]}

错误信息

note: LINK : fatal error LNK1181: cannot open input file 'SDL2_gfx.lib'

解决方案

方法1:使用预编译二进制文件

直接下载适配你当前SDL2版本的SDL2_gfx预编译包,里面会包含SDL2_gfx.lib(静态库)和SDL2_gfx.dll(动态库):

  • 将SDL2_gfx.lib复制到你提到的Rust工具链lib目录
  • 将SDL2_gfx.dll放到项目根目录,或者系统动态库搜索路径(比如C:\Windows\System32)

方法2:通过vcpkg自动安装编译

vcpkg会自动处理依赖和编译流程:

  1. 安装vcpkg后,执行对应平台的安装命令:vcpkg install sdl2-gfx:x64-windows(x86平台替换为x86-windows)
  2. 安装完成后,在vcpkg安装目录的installed\x64-windows\lib找到SDL2_gfx.lib,installed\x64-windows\bin找到SDL2_gfx.dll
  3. 按方法1的路径要求放置这两个文件

方法3:配置Cargo自动链接库

无需手动复制文件,通过build脚本指定库路径:

  1. 修改Cargo.toml:
[dependencies]
sdl2 = {
    version = "0.35.2",
    default-features = false,
    features = ["gfx"],
    build = "build.rs"
}
  1. 在项目根目录创建build.rs文件,填入以下内容(替换为你的SDL2_gfx库实际路径):
fn main() {
    println!("cargo:rustc-link-search=native=C:/path/to/sdl2_gfx/lib");
    println!("cargo:rustc-link-lib=static=SDL2_gfx");
}
  1. 将SDL2_gfx.dll放到项目根目录即可运行项目

内容的提问来源于stack exchange,提问作者Tony

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 21:23:13