2

我目前正在尝试配置我的 Cmake 文件以包含 SFML 库。我的 CMakeLists.txt。如果这很重要,我正在使用 OS X Yosemite。

cmake_minimum_required(VERSION 2.8.4)
project(SFMLTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=lib++")

set(SOURCE_FILES main.cpp)
add_executable(SFMLTest ${SOURCE_FILES})

#Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2.2 REQUIRED system window graphics network audio)
if (SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(${main.cpp} ${SFML_Libraries})
endif()

我目前得到的错误是

Error:By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has asked 
CMake to find a package configuration file provided by "SFML", but CMake did notfind one.
Could not find a package configuration file provided by "SFML" (requested version 2.2) 
with any of the following names:
 SFMLConfig.cmake   sfml-config.cmake
Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or 
set "SFML_DIR"to a directory containing one of the above files.  
If "SFML" provides a separate development package or SDK, be sure it has been installed.

我的 FindSFML.cmake 位于

/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules
4

2 回答 2

9

您的问题说 find 模块的完整路径是

/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules/FindSFML.cmake

但是您正在添加${CMAKE_SOURCE_DIR}/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules到您的CMAKE_MODULE_PATH. ${CMAKE_SOURCE_DIR}是包含顶级CMakeLists.txt文件的目录的绝对路径。如果您知道要包含的模块文件的绝对路径,则绝对不应该在其前面加上源树路径。只需将行更改为:

set(CMAKE_MODULE_PATH "/Users/Home/SFML-2.2-osx-clang-universal/cmake/Modules" ${CMAKE_MODULE_PATH})

另请注意,由于您已REQUIRED在 的参数中指定,find_package()如果找不到包,CMake 将终止并出现错误。因此,拥有if(SFML_FOUND)是没有意义的。

于 2015-01-30T19:52:34.420 回答
-1

这个解决方案是完美的:https ://oxymeos.shost.ca/article.php?about=work_with_the_SFML_in_CLion 。我推荐它!它对我有用

您在项目的根目录下创建一个名为“cmake_modules”的文件夹,并将“FindSFML.cmake”文件放入此文件夹(在 Windows 和 Mac OS X 上:“[Your_SFML Location]/cmake/Modules/FindSFML.cmake”,在 Linux 上:“[Your_SFML_location]/share/SFML/cmake/Modules/FindSFML.cmake”。

CMake 配置文件 (CMakeLists.txt) 然后以这种形式呈现:

cmake_minimum_required(VERSION 3.7)
project([your_project])

# Define the source and the executable
set(EXECUTABLE_NAME "[name_executable]")
add_executable(${EXECUTABLE_NAME} [project_files])


# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
于 2017-06-30T12:26:08.830 回答