如何在Windows上修复cmake find_package“找不到SDL2”?

问题描述 投票:1回答:1

我正在使用SDL2开发一个小项目,我正在使用cmake来构建和编译代码,以便它可以在不同的平台(主要是Windows和Linux)上运行。现在我在使用windows上的find_package函数找不到SDL2库时遇到了麻烦。

我已经基于几个博客实现了我自己的FindSDL2.cmake文件,这些博客链接到与此类似的问题,但它仍然无法找到SDL2库。

我看到的另一个解决方案是将安装SDL2的路径传递给CMAKE_PREFIX_PATH,但除非我遗漏了某些东西,否则这意味着如果我要在github上安装我的代码并且有人想要构建它,那么就自己运行它们会还必须通过他们安装SDL2的路径,这看起来有点烦人,也不是其他类似的开源项目正在做的事情。

这是我的CMakeLists.txt文件

cmake_minimum_required(VERSION 3.0.0)
project(Generic-Fighting-Game VERSION 0.1.0)

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

include(CTest)
enable_testing()

find_package(SDL2 REQUIRED)

include_directories("${SDL2_INCLUDE_DIR}")

add_executable(Generic-Fighting-Game src/main.cpp)
target_link_libraries(Generic-Fighting-Game ${SDL2_LIBRARY})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

并使用在这里找到的FindSDL2.cmake字段https://github.com/brendan-w/collector/blob/master/cmake/FindSDL2.cmake,因为我不太了解cmake以便制作我自己的FindSDL2.cmake文件

一旦我在构建目录上运行make,我就会收到以下错误

C:\Users\HP\Documents\Generic-Fighting-Game\build>make
CMake Error at C:/Program Files/CMake/share/cmake-3.14/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find SDL2 (missing: SDL2_LIBRARY SDL2_INCLUDE_DIR)
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.14/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  cmake/FindSDL2.cmake:163 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:9 (find_package)


-- Configuring incomplete, errors occurred!
See also "C:/Users/HP/Documents/Generic-Fighting-Game/build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/HP/Documents/Generic-Fighting-Game/build/CMakeFiles/CMakeError.log".
Makefile:604: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1

任何帮助表示赞赏!

c++ windows cmake sdl-2
1个回答
1
投票

以下链接FindSDL2.cmake适用于unix和mingw。所以,如果你在窗口平台上运行你的cmake,并且你可能不使用mingw路径是错误的,那就意味着不是windows格式。所以cmake找不到库。

https://github.com/brendan-w/collector/blob/master/cmake/FindSDL2.cmake

SET(SDL2_SEARCH_PATHS
    ~/Library/Frameworks
    /Library/Frameworks
    /usr/local
    /usr
    /sw # Fink
    /opt/local # DarwinPorts
    /opt/csw # Blastwave
    /opt
)

所以,让它解决。

设置窗口格式的路径,如C:\ ProgramData \ Microsoft

并设置库安装或存在路径的路径位置。

© www.soinside.com 2019 - 2024. All rights reserved.