我如何在Clion项目中包含SDL_gfx库?

问题描述 投票:0回答:2

所以我遇到了一件奇怪的事情,我可以使SDL2库在clion中工作,但是如果我包括SDL_gfx库,它将无法编译。

所以这是我到目前为止所做的:

  1. 我已经从网站上下载了SDL的Windows 64版本,将其解压缩,并将文件拖到相应的MinGW文件夹中。
  2. 我已经编辑了cmake文件,以便clion也可以看到它,并且确实可以使用

    我的cmake文件:

    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. 我已经从here下载了SDL_gfx包,并将.h和.a文件放在相应的MinGW文件夹中。 Clion可以看到它,并且看起来很正常,直到我点击编译:

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

如果我的代码是:

#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;
}

比它有用。它将编译并出现空白窗口。但是,如果我从注释中删除了这三行,那么它将无法编译,并且我将得到下一个错误:

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

奇怪的是,clion可以看到这些circleRGBA功能,在this picture中我们可以看到它可以识别功能,并在变量旁边写入属性。

cmake sdl-2
2个回答
0
投票

遗憾的是,SDL_gfx没有标准的cmake模块。但是您可以通过搜索FindSDL2_gfx.cmake在互联网上找到一个。将此文件放在您的cmake/Modules目录中。

find_package(SDL2_gfx REQUIRED)

然后使用${SDL2_GFX_LIBRARIES}进行链接


0
投票

好,所以我知道了。问题是clion无法看到.c SDL2_gfx文件,因此我不得不将它们添加到项目中,并且必须在CMakeList.txt中编辑以下行:

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

这样,它可以编译并正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.