我在Windows 10上使用SDL2和CLion构建测试项目。名为HelloSDL的项目基于this tutorial,只创建一个窗口并打印" Hello World" 。我使用here中的FindSDL2.cmake和FindSDL2_ttf.cmake脚本。我的CMakeLists.txt文件如下:
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})
该项目在MinGW下构建并运行完美,但是当我尝试在Cygwin下构建它时,我得到链接错误:
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'
我没有太多经验,但对我而言,这表明它没有链接到SDL2库。我很困惑为什么当CMake输出显示它找到了SDL2库时:
-- 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
在试图让它在Cygwin下编译时,我们将不胜感激。
答案 0 :(得分:0)
首先你需要使用Cygwin中的库(libSDL2 * -devel),而不是你为MinGW下载的库,你还需要使用MSYS格式生成带有CMake的Makefile。
cmake -G" MSYS Makefiles"