使用SFML库构建CMake项目



我不明白如何使用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++文件(它在project子文件夹中)。
…/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;
}

截屏项目根文件夹截图

SFML文件夹截图

main.cpp -source folder

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

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

cmake -S . -B build_release -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="C:UserstimDocumentsc-c++test_projSFML" -G "Ninja"

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

最新更新