如何在 CMake 中链接 SDL2?

How to link SDL2 in CMake?

本文关键字:链接 SDL2 CMake      更新时间:2023-10-16

我正在尝试在 ubuntu 上使用 sdl。根据此说明(https://gist.github.com/BoredBored/3187339a99f7786c25075d4d9c80fad5(,我安装了sdl2,sdl图像和sdl混合器。现在我必须在构建时链接它们。下面我应该怎么做的例子。

g++ myProgram.cpp -o myProgram `sdl2-config --cflags --libs` -lSDL2 -lSDL2_mixer -lSDL2_image -lSDL2_ttf

我正在使用 Cmake,我不知道如何链接它们......

下面是仅用于测试 sdl 是否正常工作的代码。

//MAIN
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
}

CMakeList 如下

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
# Add an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES})
# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_include_directories(sdl
PRIVATE 
${PROJECT_SOURCE_DIR}/inc
)

如何在 Cmake 中链接它们?谢谢你的时间。

要在cmake中链接库(共享/静态(,您可以使用 target_link_libraries 命令:

target_link_libraries(<target> ... <item>... ...)

根据文档:

<target>必须由命令(如add_executable()add_library()

创建

因此,首先我们需要找到 SDL 库,为此我们将使用以下命令:

find_package(SDL2 REQUIRED)

要使其包含目录可供您使用,请使用以下命令:

include_directories(${SDL2_INCLUDE_DIRS})

最后,要链接 SDL2,您需要执行以下操作:

target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})

或者

target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)

PRIVATE,意味着${PROJECT_NAME}在其实现中使用SDL2,但SDL2不用于${PROJECT_NAME}公共API的任何部分。更多在这里

这里${PROJECT_NAME}<target>,后面的其余都是库的名称。

最终结果

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
find_package(SDL2 REQUIRED)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
# Add an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(sdl ${SDL2_LIBRARIES})
# Set the directories that should be included in the build command for this target
include_directories(SDL2Test ${SDL2_INCLUDE_DIRS})

裁判:


  1. https://cmake.org/cmake/help/latest/command
  2. https://cmake.org/pipermail/cmake/2016-May/063400.html

EDIT 添加了完整的CMAKE

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
target_include_directories(sdl
PRIVATE 
${PROJECT_SOURCE_DIR}/inc
)
# Add an executable with the above sources
link_directories(path_to_lib)
add_executable(${PROJECT_NAME} ${SOURCES})

# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/

target_link_libraries((${PROJECT_NAME} SDL2 SDL2_mixer SDL2_image SDL2_ttf)