[英]How to link SDL2 in CMake?
I'm trying to use sdl on ubuntu. According to this instruction( https://gist.github.com/BoredBored/3187339a99f7786c25075d4d9c80fad5 ) i installed sdl2, sdl image and sdl mixer.我正在尝试在 ubuntu 上使用 sdl。根据此说明( https://gist.github.com/BoredBored/3187339a99f7786c25075d4d9c80fad5 ),我安装了 sdl2、sdl 图像和 sdl 混合器。 Now I have to link them while building.
现在我必须在构建时链接它们。 Example how should I do it below.
下面是我应该怎么做的例子。
g++ myProgram.cpp -o myProgram `sdl2-config --cflags --libs` -lSDL2 -lSDL2_mixer -lSDL2_image -lSDL2_ttf
I'm using Cmake and I have no idea how to link them...我正在使用 Cmake,我不知道如何链接它们......
Below it's code done just for testing sdl working or not.下面是仅用于测试 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 below下面的 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
)
How can I link them in Cmake?我如何在 Cmake 中链接它们? Thanks for your time.
谢谢你的时间。
To link a library (shared/static) in cmake you can use the target_link_libraries command:要链接cmake中的库(共享/静态),您可以使用target_link_libraries命令:
target_link_libraries(<target> ... <item>... ...)
According to the documentation:根据文档:
<target>
must have been created by a command such asadd_executable()
oradd_library()
<target>
必须由add_executable()
或add_library()
等命令创建
So first of all we need to find the SDL library, for that we will use the command:所以首先我们需要找到 SDL 库,为此我们将使用命令:
find_package(SDL2 REQUIRED)
to make it's include directories available to you, use the command:要使其包含目录可供您使用,请使用以下命令:
include_directories(${SDL2_INCLUDE_DIRS})
And finally to link SDL2, you need to do:最后要链接 SDL2,您需要执行以下操作:
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
or alternatively :或者:
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
PRIVATE
, means that ${PROJECT_NAME}
uses SDL2
in its implementation, but SDL2
is not used in any part of ${PROJECT_NAME}
's public API. More here PRIVATE
,意味着${PROJECT_NAME}
在其实现中使用SDL2
,但SDL2
未在${PROJECT_NAME}
的公共 API 的任何部分中使用。更多在这里
Here ${PROJECT_NAME}
is the <target>
, and all the rest that follow are names of libraries.这里
${PROJECT_NAME}
是<target>
,后面的所有 rest 都是库的名称。
# 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})
Refs:参考:
EDIT added the full CMAKE编辑添加了完整的 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.