undefined reference to `WinMain@16' error while linking SDL1.2 in cmake for MinGW

498 views Asked by At

I recently managed to find solution to this problem related to SDL2, but somehow those solutions does not apply to SDL1.2. I tried using the flags -lmingw32 -lSDL -lSDLmain and also to add add_definitions(-DSDL_MAIN_HANDLED) to the end of the CMakeLists.txt file. However I still get the following error message:

====================[ Build | Graphics_project | Debug ]========================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\Coding\Projects\Graphics_project\cmake-build-debug --target Graphics_project -- -j 4
[ 50%] Linking CXX executable Graphics_project.exe
D:/Coding/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/Coding/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o): in function `main':
D:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [Graphics_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Graphics_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Graphics_project.dir/rule] Error 2
CMakeFiles\Graphics_project.dir\build.make:85: recipe for target 'Graphics_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/Graphics_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/Graphics_project.dir/rule' failed
Makefile:117: recipe for target 'Graphics_project' failed
mingw32-make.exe: *** [Graphics_project] Error 2

Here is my CMakeLists.txt content:

cmake_minimum_required(VERSION 3.15)
project(Graphics_project)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

find_package(SDL REQUIRED)
include_directories(${SDL_INCLUDE_DIR})

add_executable(Graphics_project main.cpp)

target_link_libraries(${PROJECT_NAME} ${SDL_LIBRARY})
add_definitions(-DSDL_MAIN_HANDLED)

And in a separate cmake folder in the project folder I have a FindSDL.cmake file:

set(FIND_SDL_PATHS D:/Coding/Devs/SDL-devel-1.2.15-mingw32/SDL-1.2.15)

find_path(SDL_INCLUDE_DIR SDL
        PATH_SUFFIXES include
        PATHS ${FIND_SDL_PATHS})

find_library(SDL_LIBRARY
        NAMES SDL SDLmain
        PATH_SUFFIXES lib
        PATHS ${FIND_SDL_PATHS})

I'am trying to compile a simple "Hello World":

#include <iostream>
#include "SDL/SDL.h"

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
} 

I would appreciate some help, I just don't understand how is it different from the SDL2 library linking.

1

There are 1 answers

0
at-2500 On

You are missing the WIN32 option in add_executable().

add_executable(Graphics_project WIN32 main.cpp)

On of the quirks of Windows is that GUI applications (not console applications) replace the entry point (the first function that is called, usually main()) of your program with WinMain() which in turn calles the main() function after some setup. The WIN32 switch makes sure that this function gets generated.