0

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
arved
  • 4,401
  • 4
  • 30
  • 53
Anmol Gautam
  • 949
  • 1
  • 12
  • 27
  • Please show the output when building via `cmake` (`make VERBOSE=1`). The command you're using that's working would also be helpful so they can be compared. – Stephen Newell Mar 19 '18 at 19:02
  • The `FindSDL2.cmake` module doesn't exist in stock CMake distribution. You probably need `find_package(SDL REQUIRED)`. – arrowd Mar 19 '18 at 19:06
  • @arrowd adding `find_package(SDL REQUIRED)` gives the following error `CMake Error at /home/tarptaeya/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/173.4674.29/bin/cmake/share/cmake-3.9/Modules/FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find SDL (missing: SDL_LIBRARY SDL_INCLUDE_DIR)` – Anmol Gautam Mar 19 '18 at 19:10
  • So `cmake` can't find SDL. Please include the command you're running *that works*. – Stephen Newell Mar 19 '18 at 19:13
  • 2
    Possible duplicate of [Using SDL2 with CMake](https://stackoverflow.com/questions/28395833/using-sdl2-with-cmake) – arrowd Mar 19 '18 at 19:15
  • @StephenNewell this works g++ -std=c++14 -Wall main.cpp `pkg-config --cflags --libs sdl2` – Anmol Gautam Mar 19 '18 at 19:47
  • 1
    What is your FindSDL2.cmake? – aram Mar 20 '18 at 14:35

0 Answers0