1

I am a beginner, I was trying to get SDL2 to work in clion but I failed. I've searched on youtube and google but nothing worked. I have SDL2.dll in the same folder as main.cpp My cmake file looks like this:

cmake_minimum_required(VERSION 3.8)
project(sdlTest)

set(CMAKE_CXX_STANDARD 17)

set(SOURCE_FILES main.cpp)

add_executable(sdlTest ${SOURCE_FILES})

add_library(SDL2.dll SHARED main.cpp)

set_target_properties(SDL2.dll PROPERTIES LINKER_LANGUAGE CXX)

I don't know what I'm doing wrong.

I tried running this example code from sdl:

#include "SDL2/SDL.h"
#include <stdio.h>

int main(int argc, char* argv[]) {

SDL_Window *window;                    // Declare a pointer

SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

// Create an application window with the following settings:
window = SDL_CreateWindow(
        "An SDL2 window",                  // window title
        SDL_WINDOWPOS_UNDEFINED,           // initial x position
        SDL_WINDOWPOS_UNDEFINED,           // initial y position
        640,                               // width, in pixels
        480,                               // height, in pixels
        SDL_WINDOW_OPENGL                  // flags - see below
);

// Check that the window was successfully created
if (window == NULL) {
    // In the case that the window could not be made...
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
}

// The window is open: could enter program loop here (see SDL_PollEvent())

SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

// Close and destroy the window
SDL_DestroyWindow(window);

// Clean up
SDL_Quit();
return 0;
}

and I get this error:

CMakeFiles\sdlTest.dir/objects.a(main.cpp.obj): In function `SDL_main':
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:8: undefined reference to 
`SDL_Init'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:18: undefined reference to 
`SDL_CreateWindow'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:23: undefined reference to 
`SDL_GetError'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:29: undefined reference to 
`SDL_Delay'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:32: undefined reference to 
`SDL_DestroyWindow'
C:/Users/Dddsasul/CLionProjects/sdlTest/main.cpp:35: undefined reference to 
`SDL_Quit'
c:/mingw/bin/../lib/gcc/mingw32/5.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]: *** [sdlTest.exe] Error 1
CMakeFiles\sdlTest.dir\build.make:95: recipe for target 'sdlTest.exe' failed
CMakeFiles\Makefile2:103: recipe for target 'CMakeFiles/sdlTest.dir/all' 
failed
CMakeFiles\Makefile2:115: recipe for target 'CMakeFiles/sdlTest.dir/rule' 
failed
mingw32-make.exe[2]: *** [CMakeFiles/sdlTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/sdlTest.dir/rule] Error 2
mingw32-make.exe: *** [sdlTest] Error 2
Makefile:130: recipe for target 'sdlTest' failed

I've tried both x64 and x86 and it's the same result but at the same time clion doesn't red-text the code, it thinks it's ok:

enter image description here

Any ideas what I'm doing wrong and how to fix? THANKS!

EDIT:

Cmake looks like this

cmake_minimum_required(VERSION 3.8)
project(sdlTest)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

find_package(SDL2 REQUIRED)

set(SOURCE_FILES main.cpp)

add_executable(sdlTest ${SOURCE_FILES})

set(SDL2_LIBRARY SDL2.dll)

target_link_libraries(sdlTest ${SDL2_LIBRARYS})

I get this error:

Found package configuration file:

C:/MinGW/lib/cmake/SDL2/sdl2-config.cmake

but it set SDL2_FOUND to FALSE so package "SDL2" is considered to be NOT
FOUND.

SECOND EDIT:

sdl2-config.cmake:

# sdl2 cmake project-config input for ./configure scripts

set(prefix "/usr/local/x86_64-w64-mingw32") 
set(exec_prefix "${prefix}")
set(libdir "${exec_prefix}/lib")
set(SDL2_PREFIX "/usr/local/x86_64-w64-mingw32")
set(SDL2_EXEC_PREFIX "/usr/local/x86_64-w64-mingw32")
set(SDL2_LIBDIR "${exec_prefix}/lib")
set(SDL2_INCLUDE_DIRS "${prefix}/include/SDL2")
set(SDL2_LIBRARIES "-L${SDL2_LIBDIR}  -lmingw32 -lSDL2main -lSDL2  -mwindows")
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
Dddsasul
  • 85
  • 2
  • 10
  • 3
    The line `add_library(SDL2.dll SHARED main.cpp)` tells CMake to add library named "SDL2.dll" which should be compiled from `main.cpp`. This is definitely not what you want. See e.g. this question https://stackoverflow.com/questions/28395833/using-sdl2-with-cmake and use any answer from it. – Tsyvarev Oct 12 '18 at 11:44
  • 2
    Possible duplicate of [Using SDL2 with CMake](https://stackoverflow.com/questions/28395833/using-sdl2-with-cmake) – Tsyvarev Oct 12 '18 at 11:45
  • Tried your link and modified my cmake but I still can't get it to work – Dddsasul Oct 12 '18 at 14:41
  • 1
    Look i am a total beginner and all those links send me to other links that explain half the process and I get lost, I don't understand cmake and you marking this as duplicate doesn't help, if you want to help, give me a proper answer. I tried what it was in those links but again it makes no sense to me and no one really explains cmake and sdl well. – Dddsasul Oct 12 '18 at 14:57
  • Well, now you show us something where we could help you. I am about the last error message. The message `... but it set SDL2_FOUND to FALSE so package "SDL2" is considered to be NOT FOUND.` usually means, that given SDL2 installation is **broken**. Check that file, and if it is not too long, add its content into the question post. Actually, if your problem is just to **link** with `SDL2.dll`, you may do that without `find_package()` call . Just check this question: https://stackoverflow.com/questions/8774593/cmake-link-to-external-library. – Tsyvarev Oct 12 '18 at 15:24
  • checked that link, it said to use target_link_libraries which I already do, also I don't know what file you want me to post. The find_package was in the link you first game me so that's why I used it. – Dddsasul Oct 12 '18 at 15:35
  • 1
    The error message says about that file: `C:/MinGW/lib/cmake/SDL2/sdl2-config.cmake`, I asked you about content of that file. As for `target_link_libraries`, you use it only in conjunction with `find_package`. I said that you may try to use it without that call. – Tsyvarev Oct 12 '18 at 16:31
  • added contents of sdl2-config.cmake; without find_package is as it was in the beginning, no cmake error but all those other errors – Dddsasul Oct 12 '18 at 16:40
  • Not sure why CMake interprets your `sdl2-config.cmake` as setting `SDL2_FOUND` variable to false. If you want just to use `target_link_libraries` for link with SDL2 and are following the question https://stackoverflow.com/questions/8774593/cmake-link-to-external-library, then notice not only commands being used, but also their **arguments**. Your current code uses `${SDL2_LIBRARYS}`, but the variable `SDL2_LIBRARYS` is never set. – Tsyvarev Oct 13 '18 at 18:09

0 Answers0