LuQ232
LuQ232

Reputation: 35

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. 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...

Below it's code done just for testing sdl working or not.

//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

# 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? Thanks for your time.

Upvotes: 2

Views: 10499

Answers (2)

Waqar
Waqar

Reputation: 9366

To link a library (shared/static) in cmake you can use the target_link_libraries command:

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

According to the documentation:

<target> must have been created by a command such as add_executable() or add_library()

So first of all we need to find the SDL library, for that we will use the command:

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:

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

Here ${PROJECT_NAME} is the <target>, and all the rest that follow are names of libraries.

Final Result

# 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:

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

Upvotes: 6

yaodav
yaodav

Reputation: 1276

EDIT added the full 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)

Upvotes: 0

Related Questions