I create CMake project which uses SDL2 by following this tutorial. However, I keep getting this error
usr/bin/g++ -fdiagnostics-color=always -g /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp -o /home/myUsername/Documents/Coding/CPP/Pong/src/main
/usr/bin/ld: /tmp/ccbnY9S2.o: in function `main':
/home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:6: undefined reference to `SDL_Init'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:12: undefined reference to `SDL_CreateWindow'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:24: undefined reference to `SDL_GetWindowSurface'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:32: undefined reference to `SDL_UpdateWindowSurface'
/usr/bin/ld: /home/myUsername/Documents/Coding/CPP/Pong/src/main.cpp:34: undefined reference to `SDL_Delay'
collect2: error: ld returned 1 exit status
I'm on Ubuntu and use VSCode with CMake. Here is the structure of my project: Project Structure
Here is my main.cpp:
#include <SDL2/SDL.h>
#include <iostream>
int main()
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
std::cout << "Failed to initialize the SDL2 library\n";
return -1;
}
SDL_Window *window = SDL_CreateWindow("SDL2 Window",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
680, 480,
0);
if(!window)
{
std::cout << "Failed to create window\n";
return -1;
}
SDL_Surface *window_surface = SDL_GetWindowSurface(window);
if(!window_surface)
{
std::cout << "Failed to get the surface from the window\n";
return -1;
}
SDL_UpdateWindowSurface(window);
SDL_Delay(5000);
}
And here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
project(Pong)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(Pong src/main.cpp)
target_link_libraries(Pong ${SDL2_LIBRARIES})
Note that I did install CMake (and Ninja, because I got errors involving Ninja, that were solved by installing it), G++ and SDL2 (the libsdl2-dev package) using apt-get beforehand.
Could someone help me please?