c++ 如何在CMake中链接SDL2?

643ylb08  于 2023-03-05  发布在  其他
关注(0)|答案(2)|浏览(232)

我尝试在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中链接它们?谢谢您的时间。

wsxa1bj1

wsxa1bj11#

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

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

根据文件:
<target>必须已由add_executable()add_library()之类的命令创建
因此,首先我们需要找到SDL库,为此,我们将使用以下命令:

find_package(SDL2 REQUIRED)

要使它的include目录对您可用,请使用以下命令:

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
hmae6n7t

hmae6n7t2#

***编辑***已添加完整的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)

相关问题