Using SDL2 with CMake
Don't set the path to SDL2 by hand. Use the proper find command which uses FindSDL. Should look like:
find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)
add_executable(ChickenShooter main.cpp)
target_include_directories(ChickenShooter ${SDL2_INCLUDE_DIR})
target_link_libraries(ChickenShooter ${SDL2_LIBRARY})
If SDL2 is not found, you have to add the path to SDL2 to CMAKE_PREFIX_PATH
, that's the place where CMake looks for installed software.
If you can use Pkg-config, its use might be easier, see How to use SDL2 and SDL_image with cmake
Linking SDL2 with CMake
Thanks to Tsyvarev!
Setting the macro SDL_MAIN_HANDLED
in the source file fixed the problem.
#define SDL_MAIN_HANDLED // insert this
#include <iostream>
#include "SDL.h"
int main() {
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "Hello world" << std::endl;
return 1;
}
std::cout << "Hello world 2" << std::endl;
SDL_Quit();
return 0;
}
How to link SDL2 in CMake?
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 asadd_executable()
oradd_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})
What happened to FindSDL2 in CMake?
FindSDL2 has never appeared in CMake.
Following the reject reason in pull request #149, SDL2 ships with a SDL2Config.cmake
, which provides a cmake package.
The documentation for find_package
states that find_package(SDL2)
will behave as follows:
- Look for
FindSDL2.cmake
, use that if it exists. (module mode) - Otherwise, use the information in
SDL2Config.cmake
orsdl2-config.cmake
. (config mode)
In short, make sure that your SDL2 package has installed the SDL2Config.cmake
file and that is on your CMAKE_PREFIX_PATH
. The documentation lists the exact paths and prefixes it looks under.
Related Topics
C++ - Enum VS. Const VS. #Define
Store Functions with Different Signatures in a Map
Correct Use of Std::Cout.Precision() - Not Printing Trailing Zeros
Why Doesn't Sfinae (Enable_If) Work for Member Functions of a Class Template
Is Adding to a "Char *" Pointer Ub, When It Doesn't Actually Point to a Char Array
Does Vector::Erase() on a Vector of Object Pointers Destroy the Object Itself
How to Access the Underlying Container of Stl Container Adaptors
Is There a Difference Between Universal References and Forwarding References
Declaring the Array Size with a Non-Constant Variable
Floating Point Format for Std::Ostream
How to Check If C++ Compiler Uses Ieee 754 Floating Point Standard
Range-For-Loops and Std::Vector<Bool>
Is String::C_Str() No Longer Null Terminated in C++11
Std::Fstream Doesn't Create File