使用 SFML 库构建 CMake 项目

CMake project building with SFML library

我不明白如何使用 CMakeLists.txt 文件构建 SFML 项目。
如果你有时间,请帮助我。谢谢!)

详情

我从官方网站 (https://www.sfml-dev.org/index.php) 下载了 SFML 库,将其移至我的项目根目录(见下面的屏幕截图),使用 SFML 编写了 C++ 代码并输入了此 cmake 命令:

.../test_proj> cmake -G "Ninja" .

但是在控制台输出中我得到了这个:

CMake Warning at subdir_02/CMakeLists.txt:5 (find_package):
  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 not find one.

  Could not find a package configuration file provided by "SFML" 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.


-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/tim/Documents/c-c++/test_proj

CMakeLists.txt:

add_executable(subdir_02 main.cpp)

set(SFML_STATIC_LIBRARIES TRUE)

find_package(SFML COMPONENTS window graphics system)

target_compile_features(subdir_02 PUBLIC cxx_std_17)
target_compile_definitions(subdir_02 PRIVATE SFML_STATIC)

target_link_libraries(subdir_02 ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})

这是使用 SFML 的主要 C++ 文件(位于项目子文件夹中)。
.../test_proj/subdir_02/main.cpp:

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow app(sf::VideoMode(800, 600, 32), "Hello World - SFML");

    return 0;
}

截图

project root folder screenshot

SFML folder screenshot

main.cpp-source folder

当你有一个 CMakeLists.txt 时的愚蠢规则,其中发现依赖项 find_package():在调用 CMake 配置时将它们的安装文件夹添加到 CMAKE_PREFIX_PATH

在你的情况下,你项目的 CMake 配置应该是:

cmake -S . -B build_release -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="C:\Users\tim\Documents\c-c++\test_proj\SFML" -G "Ninja"

实际上,SFML 包在子目录中提供了一个 CMake 配置文件 SFMLConfig.cmakefind_package(SFML) 可能正在尝试查找一个文件。 CMake 不知道这个文件在哪里,所以它会查看 CMAKE_PREFIX_PATH 中列出的每个路径下的一些特定子目录,并最终查看一些标准系统路径。 实际上,搜索过程可能非常复杂 (https://cmake.org/cmake/help/latest/command/find_package.html#config-mode-search-procedure),但对于本机构建,CMAKE_PREFIX_PATH 是你的朋友。