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.