【问题标题】:How to link SDL2 in CMake?如何在 CMake 中链接 SDL2?
【发布时间】:2020-05-18 08:44:02
【问题描述】:

我正在尝试在 ubuntu 上使用 sdl。根据这个指令(https://gist.github.com/BoredBored/3187339a99f7786c25075d4d9c80fad5),我安装了 sdl2、sdl image 和 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 中链接它们?感谢您的宝贵时间。

【问题讨论】:

标签: c++ cmake linker


【解决方案1】:

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

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

根据文档:

&lt;target&gt; 必须是由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 的任何部分中使用。 More here

这里的${PROJECT_NAME}&lt;target&gt;,后面的都是库的名称。

最终结果

    # 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

【讨论】:

  • 请注意,sdl2-config.cmake 还定义了 SDL2::SDL2SDL2::SDL2-static IMPORTED 目标。将这些与 target_link_libraries 一起使用会自动设置包含目录 AND 链接器标志。
  • 请参阅it's time to do CMake right,了解为什么 IMPORTED 目标优于变量。
  • 以某种方式将 CMakeList 更改为您的后,构建时仍然存在问题。fatal error: SDL2/SDL_ttf.h: No such file or directory #include &lt;SDL2/SDL_ttf.h&gt;
  • @LuQ232 你确定你已经安装了 SDL2 并且可以在/usr/include/...找到它
  • @Waqar CMake 3.0(2014 年发布)已经知道 IMPORTED 目标,没有理由推迟 CMake 升级。
【解决方案2】:

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

【讨论】:

  • 在这一行之后我现在有这样的错误:CMake Error at CMakeLists.txt:24 (target_link_libraries): Cannot specify link libraries for target "lSDL2" which is not built by this project.
  • 添加链接目录(path_to_lib)
  • 您的target_link_libraries 有一个额外的l 用于所有库。
猜你喜欢
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
相关资源
最近更新 更多