Search code examples
windowscygwinmingwsdl-2clion

SDL2 project builds on MinGW but not Cygwin using CLion


I'm building a test project using SDL2 and CLion on Windows 10. The project, called HelloSDL is based off this tutorial, and just creates a window and prints "Hello World". I'm using the FindSDL2.cmake and FindSDL2_ttf.cmake scripts from here. My CMakeLists.txt file is as follows:

cmake_minimum_required(VERSION 3.6)
project(HelloSDL)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${HelloSDL_SOURCE_DIR}/cmake")

set(SDL2_PATH "C:\\SDL\\SDL2-2.0.5\\i686-w64-mingw32" CACHE PATH "The location to search for SDL2")
set(SDL2_TTF_PATH "C:\\SDL\\SDL2_ttf-2.0.14\\i686-w64-mingw32" CACHE PATH "The location to search for SDL2_TTF")

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})

find_package(SDL2_ttf REQUIRED)
include_directories(${SDL2_TTF_INCLUDE_DIR})

include_directories(include)

set(SOURCE_FILES main.cpp)
add_executable(HelloSDL ${SOURCE_FILES})
target_link_libraries(HelloSDL ${SDL2_LIBRARY} ${SDL2_TTF_LIBRARY})

The project builds and runs perfectly fine under MinGW, however when I try and build it under Cygwin I get linking errors:

CMakeFiles/HelloSDL.dir/main.cpp.o: In function `SDL_main':
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:97: undefined reference to `SDL_Init'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:102: undefined reference to `TTF_Init'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:104: undefined reference to `SDL_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:109: undefined reference to `SDL_CreateWindow'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:113: undefined reference to `TTF_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:114: undefined reference to `SDL_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:117: undefined reference to `SDL_CreateRenderer'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:121: undefined reference to `TTF_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:122: undefined reference to `SDL_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:133: undefined reference to `TTF_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:134: undefined reference to `SDL_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:140: undefined reference to `SDL_QueryTexture'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:148: undefined reference to `SDL_PollEvent'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:156: undefined reference to `SDL_RenderClear'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:160: undefined reference to `SDL_RenderPresent'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:164: undefined reference to `TTF_Quit'
/cygdrive/c/Users/Kieran/CLionProjects/HelloSDL/main.cpp:165: undefined reference to `SDL_Quit'

I don't have a lot of experience, but to me this indicates that it's not linking against the SDL2 library. I'm confused as to why when the CMake output indicates it found the SDL2 library:

-- Found SDL2: C:/SDL/SDL2-2.0.5/i686-w64-mingw32/lib/libSDL2main.a;C:/SDL/SDL2-2.0.5/i686-w64-mingw32/lib/libSDL2.dll.a  

Any help would be appreciated in trying to get it to compile under Cygwin.


Solution

  • First you need to use the Libraries from Cygwin (libSDL2*-devel), not the one you have downloaded for MinGW, and you also need to generate your Makefile with CMake using the MSYS format.

    cmake -G "MSYS Makefiles"