cmakesdl-2

How can i include SDL_gfx libraries in a Clion project?


So there is a weird thing that I've encountered, where i can make the SDL2 libraries work in clion but if i include the SDL_gfx libraries it won't compile.

So here is what I've done so far:

  1. I've downloaded the Windows 64 version of SDL from the website, uncompressed it, and dragged the files in the corresponding MinGW folder.
  2. I've edited the cmake file so that clion can see it as well, and it really works

    My cmake file:

    cmake_minimum_required(VERSION 3.15)
    project(pontok C)
    
    
    set(CMAKE_C_STANDARD 99)
    set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${PROJECT_SOURCE_DIR}/cmake")
    
    find_package(SDL2 REQUIRED)
    
    add_executable(pontok main.c)
    
    include_directories(${SDL2_INCLUDE_DIR}
            ${SDL2_IMAGE_INCLUDE_DIR}
            ${SDL2_MIXER_INCLUDE_DIR}
            )
    
    target_link_libraries(pontok ${SDL2_LIBRARY}
            ${SDL2_IMAGE_LIBRARY}
            ${SDL2_MIXER_LIBRARY}
            )
    
  3. I've downloaded the SDL_gfx package from here and put the .h and .a files in the corresponding MinGW folders. Clion can see it, and it appears normal until I hit compile:

    Here you can see, that it is not highlighted, and it does nit have a problem with it.

If my code is:

#include <stdio.h>
#include <math.h>
#include <SDL2\SDL2_gfxPrimitives.h>
#include <SDL2\SDL.h>

const int SCREEN_WIDH = 640;
const int SCREEN_HEIGHT = 480;

int main(int argc, char *argv[]) {
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        SDL_Log("Nem indithato az SDL: %s", SDL_GetError());
        exit(1);
    }
    SDL_Window *window = SDL_CreateWindow("SDL peldaprogram", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 440, 360, 0);
    if (window == NULL) {
        SDL_Log("Nem hozhato letre az ablak: %s", SDL_GetError());
        exit(1);
    }
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
    if (renderer == NULL) {
        SDL_Log("Nem hozhato letre a megjelenito: %s", SDL_GetError());
        exit(1);
    }
    SDL_RenderClear(renderer);

    int x, y, r;

    //circleRGBA(renderer, x, y, r, 255, 0, 0, 255);
    //circleRGBA(renderer, x + r, y, r, 0, 255, 0, 255);
    //circleRGBA(renderer, x + r * cos(3.1415 / 3), y - r * sin(3.1415 / 3), r, 0, 0, 255, 255);

    SDL_RenderPresent(renderer);

    SDL_Event ev;
    while (SDL_WaitEvent(&ev) && ev.type != SDL_QUIT) {
    }

    SDL_Quit();
    return 0;
}

Than it works. It compiles and the blank window appears. But if i remove those three lines from comments, then it wont compile, and I'll get the next error:

CMakeFiles\pontok.dir/objects.a(main.c.obj): In function `SDL_main':
C:/Prog/pontok/main.c:28: undefined reference to `circleRGBA'
C:/Prog/pontok/main.c:29: undefined reference to `circleRGBA'
C:/Prog/pontok/main.c:30: undefined reference to `circleRGBA'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\pontok.dir\build.make:88: pontok.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/pontok.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/pontok.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: pontok] Error 2

The weird thing is that clion can see these circleRGBA unctions, in this picture we can see that it can recognize the function, it writes the attributes next to the variables.


Solution

  • Ok so i figured it out. The problem was that clion could not see the .c SDL2_gfx files, so i had to add them to the project and had to edit the following line in the CMakeList.txt:

    add_executable(pontok main.c SDL2_framerate.c SDL2_gfxPrimitives.c SDL2_imageFilter.c SDL2_rotozoom.c)

    This way it compiled and worked properly.