I am exploring SDL and wrote the following simple code
#include <SDL2/SDL.h>
int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0){
return 0;
}
SDL_Window* window = SDL_CreateWindow("Hello World",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
600,
400,
SDL_WINDOW_SHOWN);
if (window == nullptr) {
return 0;
}
SDL_Surface* surface = SDL_GetWindowSurface(window);
SDL_FillRect(surface,
nullptr,
SDL_MapRGB(
surface->format,
0xFF,
0xFF,
0xFF));
SDL_UpdateWindowSurface(window);
SDL_Delay(9000);
SDL_DestroyWindow(window);
return 0;
}
And have the following cmake file for Clion
cmake_minimum_required(VERSION 3.9)
project(LazyFoo)
set(CMAKE_CXX_STANDARD 14)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(LazyFoo main.cpp)
target_link_libraries(LazyFoo ${SDL2_LIBRARY})
The problem is that when I am building my main.cpp directly from the command line without using cmake, then the code compiles and a nice window is shown, but when I use clion to build the code then it throws undefined reference to the all the SDL functions which I used above.
I know that the problem lies with my cmake file and have googled for the same but all was in vain.
How should I change my cmake file to be able to build the project from the CLion itself.
EDIT My clion build message
/home/tarptaeya/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/173.4674.29/bin/cmake/bin/cmake --build /home/tarptaeya/DevGames/LazyFoo/cmake-build-debug --target LazyFoo -- -j 2
Scanning dependencies of target LazyFoo
[ 50%] Building CXX object CMakeFiles/LazyFoo.dir/main.cpp.o
[100%] Linking CXX executable LazyFoo
CMakeFiles/LazyFoo.dir/main.cpp.o: In function `main':
/home/tarptaeya/DevGames/LazyFoo/main.cpp:4: undefined reference to `SDL_Init'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:12: undefined reference to `SDL_CreateWindow'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:16: undefined reference to `SDL_GetWindowSurface'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:17: undefined reference to `SDL_MapRGB'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:23: undefined reference to `SDL_FillRect'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:24: undefined reference to `SDL_UpdateWindowSurface'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:25: undefined reference to `SDL_Delay'
/home/tarptaeya/DevGames/LazyFoo/main.cpp:26: undefined reference to `SDL_DestroyWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/LazyFoo.dir/build.make:94: recipe for target 'LazyFoo' failed
make[3]: *** [LazyFoo] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/LazyFoo.dir/all' failed
make[2]: *** [CMakeFiles/LazyFoo.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/LazyFoo.dir/rule' failed
make[1]: *** [CMakeFiles/LazyFoo.dir/rule] Error 2
Makefile:118: recipe for target 'LazyFoo' failed
make: *** [LazyFoo] Error 2