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

Linux环境下CMake中GLUT已链接但SDL链接失败的解决求助

Fixing SDL Linking Issues in Linux CMake Projects

Hey there, let's break down why your SDL linking is failing and fix it up. The core problem here is a version mismatch: your code is using SDL 1.2 APIs (like SDL_SetVideoMode, the old SDL_Init signature), but your CMake config is trying to link against SDL2. These two versions have completely different library structures and API signatures, so the linker can't find those old function references.

Here are two paths to fix this:

Option 1: Stick with SDL 1.2 (Match Your Current Code)

If you don't want to rewrite your code for SDL2, update your CMakeLists.txt to properly locate SDL 1.2 instead of SDL2:

cmake_minimum_required(VERSION 3.7)
project(ObjLoader)
add_executable(testas main.cpp)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
# Look for SDL 1.2 instead of SDL2
find_package(SDL REQUIRED)

include_directories(
    ${OPENGL_INCLUDE_DIRS}
    ${GLUT_INCLUDE_DIRS}
    ${SDL_INCLUDE_DIR}  # Note the singular variable name for SDL 1.2
)

target_link_libraries(testas
    ${OPENGL_LIBRARIES}
    ${GLUT_LIBRARY}
    ${SDL_LIBRARY}      # Singular variable name here too
)

If your system doesn't have the SDL 1.2 CMake module installed, you can use the sdl-config tool (included with SDL 1.2 dev packages) to grab the right compiler/linker flags:

cmake_minimum_required(VERSION 3.7)
project(ObjLoader)
add_executable(testas main.cpp)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)

# Pull compile and link flags from sdl-config
execute_process(
    COMMAND sdl-config --cflags
    OUTPUT_VARIABLE SDL_CFLAGS
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
    COMMAND sdl-config --libs
    OUTPUT_VARIABLE SDL_LIBS
    OUTPUT_STRIP_TRAILING_WHITESPACE
)

# Apply the flags to your target
target_compile_options(testas PRIVATE ${SDL_CFLAGS})
target_link_libraries(testas
    ${OPENGL_LIBRARIES}
    ${GLUT_LIBRARY}
    ${SDL_LIBS}
)

SDL 1.2 is no longer maintained, so moving to SDL2 is a better long-term choice. You'll need to update your code to use SDL2's APIs:

  • Replace SDL_SetVideoMode with SDL_CreateWindow + SDL_GL_CreateContext (since you're using OpenGL)
  • Update your event loop to use SDL2's event handling
  • Make sure your code includes <SDL2/SDL.h> instead of <SDL/SDL.h>

Once your code is updated, your original SDL2 CMake config should work—just make sure you have the SDL2 dev package installed on your system (e.g., sudo apt install libsdl2-dev on Ubuntu/Debian):

cmake_minimum_required(VERSION 3.7)
project(ObjLoader)
add_executable(testas main.cpp)

find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(SDL2 REQUIRED)

include_directories(
    ${OPENGL_INCLUDE_DIRS}
    ${GLUT_INCLUDE_DIRS}
    ${SDL2_INCLUDE_DIRS}
)

target_link_libraries(testas
    ${OPENGL_LIBRARIES}
    ${GLUT_LIBRARY}
    ${SDL2_LIBRARIES}
)

Quick Checks to Verify

  1. Install the right dev package:
    • SDL1.2: sudo apt install libsdl1.2-dev (Debian/Ubuntu)
    • SDL2: sudo apt install libsdl2-dev (Debian/Ubuntu)
  2. Check CMake's output: When you run cmake .., look for lines like -- Found SDL: /usr/lib/x86_64-linux-gnu/libsdl.so (SDL1.2) or -- Found SDL2: /usr/lib/x86_64-linux-gnu/libsdl2.so (SDL2). If you don't see these, the library isn't installed or CMake can't find it.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:37:56