0

I'm trying to set up a project C++ project in CLion using SDL2. Currently, I copied all of the SDL2 files into the appropriate folders in MinGW, and I copied SDL2.dll into the build directory where the executable is generated. My CmakeLists file looks like this:

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)

And my main.cpp file looks like this (pretty much copied from the SDL2 tutorial):

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

I finally got CLion to recognize SDL2, and it doesn't show any errors in my source files, but when I compile, I get this error:

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

I've followed many different tutorials, started over from scratch several times, and nothing has worked so far.

Thomas
  • 1,123
  • 3
  • 13
  • 36
  • 1
    Not sure why `-mwindows` option is used, try to build without it. Also, you incorrectly use results of `find_package(SDL2)` call. (That is, you don't use these results at all). After that call, variables `SDL2_INCLUDE_DIRS` and `SDL2_LIBRARIES` are set, so use them like described here: https://stackoverflow.com/a/44347594/3440745. – Tsyvarev Nov 11 '18 at 19:57
  • Where did you get the precompiled SDL2? Are you sure you got the correct one? (i686 if you compile for x32, x86_64 if you compile for x64) – HolyBlackCat Nov 11 '18 at 19:58
  • @Tsyvarev The only effect `-mwindows` seems to have is that it prevents your program from opening a terminal window when you start it. I'm successfully building my SDL2 project with it. – HolyBlackCat Nov 11 '18 at 19:59
  • @HolyBlackCat: Yes, I saw [that question](https://stackoverflow.com/questions/2752792/whats-the-equivalent-of-gccs-mwindows-option-in-cmake) about `-mwindows` option. In the given case it seems for me that setup for the project is wrong (it is unknown which SDL2 library the asker has), and I worried that `-mwindows` adds more messes in the setup. So I would suggest to remove it until all other problems be resolved. – Tsyvarev Nov 11 '18 at 20:09

0 Answers0