在Windows / MinGW上设置SDL2时出现问题

时间:2018-11-11 19:21:04

标签: c++ cmake sdl-2 clion

我正在尝试使用SDL2在CLion中设置项目C ++项目。当前,我将所有SDL2文件复制到MinGW中的相应文件夹中,并将SDL2.dll复制到生成可执行文件的生成目录中。我的CmakeLists文件看起来像这样:

cmake_minimum_required(VERSION 3.12)
project(MyProject)

set(CMAKE_CXX_STANDARD 17)

# Add SDL2
find_package(SDL2 REQUIRED)

add_executable(MyProject main.cpp)

target_link_libraries(MyProject SDL2 -lmingw32 -lSDL2main -lSDL2 -mwindows)

我的main.cpp文件看起来像这样(从SDL2教程中复制了差不多):

#include <SDL2/SDL.h>

int main(int argc, char *argv[]) {
    const int SCREEN_WIDTH = 640;
    const int SCREEN_HEIGHT = 480;

    SDL_Window* window = nullptr;
    SDL_Surface* screenSurface = nullptr;

    //Initialize SDL
    if(SDL_Init( SDL_INIT_VIDEO ) < 0) {
        printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    }else { //Create window
        window = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );

        if( window == nullptr ) {
            printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
        }
    }

    return 0;
}

我终于让CLion识别SDL2,并且它在我的源文件中没有显示任何错误,但是当我编译时,出现了此错误:

CMakeFiles\MyProject.dir/objects.a(main.cpp.obj): In function `SDL_main':
D:/Documents/Programming Projects/MyProject/main.cpp:14: undefined reference to `SDL_Init'
D:/Documents/Programming Projects/MyProject/main.cpp:15: undefined reference to `SDL_GetError'
D:/Documents/Programming Projects/MyProject/main.cpp:17: undefined reference to `SDL_CreateWindow'
D:/Documents/Programming Projects/MyProject/main.cpp:20: undefined reference to `SDL_GetError'
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [MyProject.exe] Error 1

我遵循了许多不同的教程,从头开始了几次,到目前为止,没有任何效果。

0 个答案:

没有答案