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

Linux编译Windows SDL2项目报错:SDL_Vertex与SDL_RenderGeometry未找到

SDL2跨平台编译错误:SDL_Vertex和SDL_RenderGeometry未定义

我在Windows系统下基于SDL2开发的项目,迁移到Linux系统编译时出现系列错误,核心提示SDL_Vertex和SDL_RenderGeometry未在作用域中声明,伴随模板参数无效等衍生错误。已通过apt安装libsdl2,具体编译命令及错误信息如下:

reilbas@reilbas:~/C/a/Labyrinthe-3D-main$ g++ -o test -I include src/*.cpp $(sdl2-config --cflags --libs)
src/Affichage.cpp: In member function ‘void Affichage::drawRect(float, float, float, float, SDL_Color&)’:
src/Affichage.cpp:110:17: error: ‘SDL_Vertex’ was not declared in this scope; did you mean ‘SDL_mutex’?
  110 |     std::vector<SDL_Vertex> triUi1 = {
      |                 ^~~~~~~~~~
      |                 SDL_mutex
src/Affichage.cpp:110:27: error: template argument 1 is invalid
  110 |     std::vector<SDL_Vertex> triUi1 = {
      |                           ^
src/Affichage.cpp:110:27: error: template argument 2 is invalid
src/Affichage.cpp:110:29: error: scalar object ‘triUi1’ requires one element in initializer
  110 |     std::vector<SDL_Vertex> triUi1 = {
      |                             ^~~~~~
src/Affichage.cpp:115:27: error: template argument 2 is invalid
  115 |     std::vector<SDL_Vertex> triUi2 = {
      |                           ^
src/Affichage.cpp:115:29: error: scalar object ‘triUi2’ requires one element in initializer
  115 |     std::vector<SDL_Vertex> triUi2 = {
      |                             ^~~~~~
src/Affichage.cpp:120:5: error: ‘SDL_RenderGeometry’ was not declared in this scope; did you mean ‘SDL_RenderCopy’?
  120 |     SDL_RenderGeometry(renderer, nullptr, triUi1.data(), triUi1.size(), nullptr, 0);
      |     ^~~~~~~~~~~~~~~~~~
      |     SDL_RenderCopy
src/Affichage.cpp: In member function ‘void Affichage::displayTri(std::vector<triangle>)’:
src/Affichage.cpp:146:21: error: ‘SDL_Vertex’ was not declared in this scope; did you mean ‘SDL_mutex’?
  146 |         std::vector<SDL_Vertex> verts = {
      |                     ^~~~~~~~~~
      |                     SDL_mutex
src/Affichage.cpp:146:31: error: template argument 1 is invalid
  146 |         std::vector<SDL_Vertex> verts = {
      |                               ^
src/Affichage.cpp:146:31: error: template argument 2 is invalid
src/Affichage.cpp:146:33: error: scalar object ‘verts’ requires one element in initializer
  146 |         std::vector<SDL_Vertex> verts = {
      |                                 ^~~~~
src/Affichage.cpp:151:9: error: ‘SDL_RenderGeometry’ was not declared in this scope; did you mean ‘SDL_RenderCopy’?
  151 |         SDL_RenderGeometry( renderer, nullptr, verts.data(), verts.size(), nullptr, 0);
      |         ^~~~~~~~~~~~~~~~~~
      |         SDL_RenderCopy
src/AllMath.cpp: In static member function ‘static int AllMath::triangleClipAgainstPlane(vec3d, vec3d, triangle&, triangle&, triangle&)’:
src/AllMath.cpp:254:1: warning: control reaches end of non-void function [-Wreturn-type]
  254 | }
      | ^

我使用的安装命令:

$ sudo apt-get install libsdl2

问题原因及解决办法

  • 核心原因:SDL_Vertex和SDL_RenderGeometry是SDL 2.0.18及以上版本才引入的API,你通过apt install libsdl2安装的仅为运行时库,且版本可能低于要求,缺少编译所需的完整头文件和API支持。
  • 解决步骤:
    1. 检查当前SDL2版本:
      sdl2-config --version
      
    2. 安装SDL2开发包(包含编译所需的头文件和链接库):
      sudo apt-get install libsdl2-dev
      
    3. 若系统仓库版本仍低于2.0.18,可从源码编译安装最新版:
      # 下载并解压最新SDL2源码包(替换为实际版本号)
      tar -xvf SDL2-2.28.5.tar.gz
      cd SDL2-2.28.5
      ./configure
      make
      sudo make install
      
    4. 修复AllMath.cpp的返回值警告:检查triangleClipAgainstPlane函数,确保所有分支都有明确的return语句,返回对应的int类型值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 05:10:20