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. 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
我正在使用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中链接它们?谢谢你的时间。
To link a library (shared/static) in cmake you can use the target_link_libraries command:
根据文档:
因此,首先我们需要找到SDL库,为此,我们将使用以下命令:
要使其包含对您可用的目录,请使用以下命令:
最后,要链接SDL2,您需要执行以下操作:
Here
${PROJECT_NAME}
is the<target>
, and all the rest that follow are names of libraries.最后结果
编辑添加了完整的CMAKE