使用 Cargo 编译基于 sdl 的 crate 会导致链接器错误(sdl2、pix-engine)[复制]

Compiling sdl-based crates with Cargo causes linker errors (sdl2, pix-engine) [duplicate]

提问人:joeman 提问时间:6/23/2023 最后编辑:cafce25joeman 更新时间:6/23/2023 访问量:76

问:

我正在运行 Ubuntu 20.04.6,通过 rustup 安装程序脚本 (rustup 1.26.0) 安装了 cargo 1.70.0

使用粘贴到 main.rs 中的pix-engine示例程序运行给了我这个错误cargo run

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH= // trim (massive list of environment variables)
  = note: /usr/bin/ld: cannot find -lSDL2
          /usr/bin/ld: cannot find -lSDL2_mixer
          /usr/bin/ld: cannot find -lSDL2_image
          /usr/bin/ld: cannot find -lSDL2_ttf
          /usr/bin/ld: cannot find -lSDL2_gfx
          collect2: error: ld returned 1 exit status

我尝试重新安装build-essential,我尝试重新安装rustup(确保它确实删除了货物二进制文件)

我还尝试了 sdl2 库(使用示例程序)并遇到了相同的错误,但

// trim
  = note: /usr/bin/ld: cannot find -lSDL2
          collect2: error: ld returned 1 exit status

在最后。因此,SDL2 也抛出链接器错误,只是更少。

Linux 链接器错误 SDL-2 Rust 货物

评论


答:

0赞 joeman 6/23/2023 #1

我在 apt 中搜索了 sdl2 软件包,直到我发现这个:

$ apt search libsdl2
Sorting... Done
Full Text Search... Done
libsdl2-2.0-0/focal,now 2.0.10+dfsg1-3 amd64 [installed]
  Simple DirectMedia Layer

libsdl2-dev/focal,now 2.0.10+dfsg1-3 amd64 [installed]
  Simple DirectMedia Layer development files

libsdl2-doc/focal,focal 2.0.10+dfsg1-3 all
  Reference manual for libsdl2

libsdl2-gfx-1.0-0/focal,now 1.0.4+dfsg-3 amd64 [installed,automatic]
  drawing and graphical effects extension for SDL2

libsdl2-gfx-dev/focal,now 1.0.4+dfsg-3 amd64 [installed]
  development files for SDL2_gfx

libsdl2-gfx-doc/focal,focal 1.0.4+dfsg-3 all
  documentation files for SDL2_gfx

libsdl2-image-2.0-0/focal,now 2.0.5+dfsg1-2 amd64 [installed,automatic]
  Image loading library for Simple DirectMedia Layer 2, libraries

libsdl2-image-dev/focal,now 2.0.5+dfsg1-2 amd64 [installed]
  Image loading library for Simple DirectMedia Layer 2, development files

libsdl2-mixer-2.0-0/focal,now 2.0.4+dfsg1-2build1 amd64 [installed,automatic]
  Mixer library for Simple DirectMedia Layer 2, libraries

libsdl2-mixer-dev/focal,now 2.0.4+dfsg1-2build1 amd64 [installed]
  Mixer library for Simple DirectMedia Layer 2, development files

libsdl2-net-2.0-0/focal 2.0.1+dfsg1-4 amd64
  Network library for Simple DirectMedia Layer 2, libraries

libsdl2-net-dev/focal 2.0.1+dfsg1-4 amd64
  Network library for Simple DirectMedia Layer 2, development files

libsdl2-ttf-2.0-0/focal,now 2.0.15+dfsg1-1 amd64 [installed,automatic]
  TrueType Font library for Simple DirectMedia Layer 2, libraries

libsdl2-ttf-dev/focal,now 2.0.15+dfsg1-1 amd64 [installed]
  TrueType Font library for Simple DirectMedia Layer 2, development files

当我(通过apt)安装时,当我重新编译时,错误部分消失了,所以我继续安装,,和libsdl2-dev/usr/bin/ld: cannot find -lSDL2libsdl2-mixer-devlibsdl2-image-devlibsdl2-ttf-devlibsdl2-gfx-dev

在那之后,它编译并完美地运行!

所以看起来

/usr/bin/ld: cannot find -lSDL2链接器错误通过安装、libsdl2-dev

-lSDL2_mixer链接器错误,带libsdl2-mixer-dev

等。

这也解决了我所拥有的 sdl2 板条箱的相同问题。

附言 我使用了 sdl2 0.35.2 和 pix-engine 0.7.0,当前最新版本

评论

3赞 Caesar 6/23/2023
只是为了形式起见,您可能应该将问题帖子的答案部分移到实际答案上。