如何在CMake中链接SDL2库?以及如何为自定义SDL2封装库配置SDL2的CMake链接
Great questions! Let's break this down step by step using modern CMake practices—this is the cleanest, most maintainable approach for C++ projects these days.
1. Linking a Library with SDL2 in CMake
Here's how to connect your library to SDL2 properly:
Step 1: Ensure SDL2 is available
You have two options here:- Install SDL2 system-wide: Use your package manager (e.g.,
apt install libsdl2-devon Ubuntu,brew install sdl2on macOS, or download precompiled binaries for Windows). - Use a self-contained submodule: Add SDL2 as a Git submodule to your project (
git submodule add https://github.com/libsdl-org/SDL.git external/SDL) so everyone on your team uses the exact same version.
- Install SDL2 system-wide: Use your package manager (e.g.,
Step 2: Find SDL2 in your CMakeLists.txt
Modern SDL2 provides CMake config files, so you can usefind_packageto locate it:find_package(SDL2 REQUIRED)This will import the official CMake targets
SDL2::SDL2(the main library) andSDL2::SDL2main(needed for Windows entry points, if you're targeting that platform).Step 3: Link your library to SDL2
Usetarget_link_librariesto connect your library target to SDL2. UsePRIVATEif your library's internal code uses SDL2 but doesn't expose SDL2 types/functions in its public API. UsePUBLICif your library's headers include SDL2 code (so dependencies propagate to projects using your library).Example CMakeLists.txt for your library:
cmake_minimum_required(VERSION 3.16) project(my_game_lib) # Locate SDL2 find_package(SDL2 REQUIRED) # Create your library add_library(my_game_lib STATIC src/renderer.cpp src/window_manager.cpp ) # Link SDL2 to your library target_link_libraries(my_game_lib PRIVATE SDL2::SDL2) # Expose your library's public headers target_include_directories(my_game_lib PUBLIC include)
2. Linking a Custom SDL2 Wrapper Library (and Propagating Dependencies)
For your SDL2 wrapper, the goal is to hide SDL2 details from your game engine while making sure the engine still gets all the necessary SDL2 dependencies automatically. Here's how:
Step 1: Configure your wrapper's CMakeLists.txt
The critical detail here is usingPUBLIC(instead ofPRIVATE) when linking SDL2 to your wrapper. This tells CMake that any target linking your wrapper also needs SDL2—so you don't have to repeat SDL2 setup in every project using the wrapper.Example for a static wrapper library:
cmake_minimum_required(VERSION 3.16) project(sdl_wrapper) # Find SDL2 (or use a submodule, see below) find_package(SDL2 REQUIRED) add_library(sdl_wrapper STATIC src/sdl_renderer_wrapper.cpp src/sdl_window_wrapper.cpp ) # Use PUBLIC to propagate SDL2 dependencies upward target_link_libraries(sdl_wrapper PUBLIC SDL2::SDL2) # Expose the wrapper's public headers target_include_directories(sdl_wrapper PUBLIC include)Step 2: Use the wrapper in your game engine
Your engine only needs to link to the wrapper—CMake will automatically pull in SDL2 dependencies for you:cmake_minimum_required(VERSION 3.16) project(my_2d_engine) # Add your wrapper as a subdirectory add_subdirectory(external/sdl_wrapper) add_executable(my_engine src/main.cpp src/engine_core.cpp) # Just link the wrapper—SDL2 is included automatically! target_link_libraries(my_engine PRIVATE sdl_wrapper)Bonus: Using SDL2 as a submodule
If you want to avoid system-wide SDL2 installs, add SDL2 as a submodule and link directly to its targets instead of usingfind_package:# In your root CMakeLists.txt add_subdirectory(external/SDL) # In your wrapper's CMakeLists.txt target_link_libraries(sdl_wrapper PUBLIC SDL2::SDL2 SDL2::SDL2main)
内容的提问来源于stack exchange,提问作者c-devhax




