0

I've been trying to set up SDL2 and SDL2_image (developer packs) on Clion Windows. After trying to compile my compiler throws error:

C:/MinGW/bin/../lib/gcc/mingw32/5.1.0/../../../libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to WinMain@16

and a lot of undefined references. I tried putting -lmingw32 -lSDL2main -lSDL2 -mwindows -lSDL2_image flags in my CMakeLists and int main(int argc, char **argv) in main function but the error keeps reoccurring.

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.4)

#Configuration parameters of cmake
set(CMAKE_CXX_COMPILER g++)#requires g++ to be available on PATH
set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(Project)

enable_language(CXX)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 
set(CMAKE_VERBOSE_MAKEFILE TRUE) 
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

#Source files here
set(SOURCE_FILES
        textures
        include/Character.h
        src/Character.cpp
        include/TextureMenager.h
        src/TextureMenager.cpp
        include/Gameboard.h
        src/Gameboard.cpp
        include/Tile.h
        src/Tile.cpp
        include/Game.h
        src/Game.cpp
        src/main.cpp)


add_executable(Project ${SOURCE_FILES})

target_include_directories(Project PUBLIC include)

find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})
target_link_libraries(Project ${SDL2_LIBRARIES} -lmingw32 -lSDL2main -lSDL2 -mwindows ${SDL2_IMAGE_LIBRARIES}  -lSDL2_image)

I also added FindSDL2.cmake and FindSDL2_image.cmake in my cmake folder.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
J B-Sz
  • 1
  • Are you using SDL to create the context window and everything? (SDL_CreateWindow/ SDL_CreateRenderer/ etc?) I don't believe you need to link against mwindows if so. – Acorn Jan 24 '20 at 20:31
  • Do your `find_package()` commands even find the SDL2 packages? What is the contents of `${SDL2_LIBRARIES}`? – Kevin Jan 24 '20 at 21:33
  • Are you sure you're using the correct libraries? The archive you downloaded includes both x32 and x64 libraries, did you try both? – HolyBlackCat Jan 25 '20 at 09:14
  • @squareskittles `find_package() ` finds both libraries. How do I check contents of `${SDL2_LIBRARIES}`? @Acorn The project compiles under Linux on my friend's computer. And yes, SDL is used to create window. Does that mean i should remove this flag? Are flags put correctly in my cmakelist? – J B-Sz Jan 25 '20 at 10:55
  • @HolyBlackCat I'm pretty sure I use correct version (x64) – J B-Sz Jan 25 '20 at 10:56
  • @JB-Sz Try the x32 one, just to be sure. When I had this exact problem, that was the reason. – HolyBlackCat Jan 25 '20 at 10:57
  • @HolyBlackCat the project compiled, by returned `Process finished with exit code -1073741819 (0xC0000005)` and doesn't display correct output. It works under Linux, using CLion on my friend's computer. – J B-Sz Jan 25 '20 at 11:26
  • @JB-Sz `0xC0000005` means segmentation fault. Use your debugger to figure out where exactly it crashes, and why. – HolyBlackCat Jan 25 '20 at 11:32
  • @HolyBlackCat thank you very mych! Everything works now. – J B-Sz Jan 25 '20 at 11:49
  • @JB-Sz If you solved the problem, please consider writing an answer post to describe what fixed the issue. This way, other visitors to this page with a similar problem can benefit from it and understand exactly what solved it. Thanks! – Kevin Jan 25 '20 at 14:25

0 Answers0