2

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.

2

2 Answers 2

6

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
Sign up to request clarification or add additional context in comments.

7 Comments

Note that sdl2-config.cmake also defines the SDL2::SDL2 and SDL2::SDL2-static IMPORTED targets. Using these with target_link_libraries automatically sets up the include directories AND linker flags.
See it's time to do CMake right as for why IMPORTED targets are preferred over the variables.
After changing CMakeList to yours somehow there is still problem while building.fatal error: SDL2/SDL_ttf.h: No such file or directory #include <SDL2/SDL_ttf.h>
@LuQ232 are you sure you have SDL2 installed and can find it in /usr/include/...
@Waqar CMake 3.0 (released 2014) already knew about IMPORTED targets, and there is little reason to delay CMake upgrades.
|
0

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) 

3 Comments

After this line now I have error like this:CMake Error at CMakeLists.txt:24 (target_link_libraries): Cannot specify link libraries for target "lSDL2" which is not built by this project.
add link_directories(path_to_lib)
Your target_link_libraries has an extra l for all the libraries.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.