我不明白如何使用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;
}
当你有一个CMakeLists.txt
发现依赖项时的愚蠢规则:在调用 CMake 配置时find_package()
添加它们的安装文件夹。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.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
来说是您的朋友。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句