将sfml与cmake链接(Windows MinGW) [英] linking sfml with cmake (Windows MinGW)
问题描述
我似乎无法使用 cmake
将 SFML
链接到我的可执行文件。
I can't seem to link SFML
to my executable using cmake
.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(Tut3)
set(LIBS_DIR ~/../../Libraries)
add_executable(Tut3 main.cpp)
set(CMAKE_MODULE_PATH ${LIBS_DIR}/sfml/cmake/Modules)
find_package(SFML REQUIRED system window graphics)
target_link_libraries(Tut3 ${SFML_LIBRARIES})
运行cmake时出现错误:
CMake错误在C:/Libraries/sfml/cmake/Modules/FindSFML.cmake:355
(消息):找不到SFML(丢失:SFML_SYSTEM_LIBRARY
SFML_WINDOW_LIBRARY SFML_GRAPHICS_LIBRARY)
CMake Error at C:/Libraries/sfml/cmake/Modules/FindSFML.cmake:355 (message): Could NOT find SFML (missing: SFML_SYSTEM_LIBRARY SFML_WINDOW_LIBRARY SFML_GRAPHICS_LIBRARY)
调用堆栈(最近调用一次):CMakeLists.txt:9(find_package)
Call Stack (most recent call first): CMakeLists.txt:9 (find_package)
sfml目录包含一个32位的 MinGW
编译的 sfml
存储库。我正在使用Windows。我使用的cmake命令是:
the sfml directory contains a 32bit MinGW
compiled sfml
repository. I am using Windows. The cmake command I use is:
cmake -G "MinGW Makefiles" ..dir..
推荐答案
查找SFML的模块与它自己的位置无关。取而代之的是,它将尝试一些常见的路径(非Windows系统)以及一些特定的变量来尝试查找实际的库。
The module to look for SFML won't look relative to its own position. Instead, it will try a few common paths (non-Windows systems) in addition to a few specific variables to try and find the actual library.
要解决此问题,您可以应该做两件事:
To solve this, you should do two things:
- 将
FindSFML.cmake
脚本移动到子目录您自己的项目,例如cmake / FindSFML.cmake
并相应地调整CMAKE_MODULE_PATH
值。 - 添加一个新的CMake变量
SFML_ROOT
指向安装SFML的目录(在您的情况下C:/ Libraries / sfml
) 。不应将其硬编码在CMakeLists.txt
文件中,而应在调用CMake时传递一次(即cmake -DSFML_ROOT = C:/。)。 。
- Move the
FindSFML.cmake
script to a sub directory of your own project, e.g.cmake/FindSFML.cmake
and adjust theCMAKE_MODULE_PATH
value accordingly. - Add a new CMake variable
SFML_ROOT
pointing to the directory where you installed SFML (in your caseC:/Libraries/sfml
). This shouldn't be hardcoded in theCMakeLists.txt
file and instead be passed once when invoking CMake (i.e.cmake -DSFML_ROOT=C:/...
; this is saved in the cache).
此外,...的结构还存在一些问题您的 CMakeLists.txt
。您应该改用以下方法:
Also there are a few problems with the structure of your CMakeLists.txt
. You should use this instead:
cmake_minimum_required(VERSION 3.0.0)
project(Tut3)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) # Tell CMake where to find the module
find_package(SFML REQUIRED COMPONENTS graphics window system) # Look for SFML
include_directories(${SFML_INCLUDE_DIR}) # You forgot this line above; add SFML's include dir
add_executable(Tut3 main.cpp) # Define the target
target_link_libraries(Tut3 ${SFML_LIBRARIES} ${SFML_DEPENDENCIES}) # Link SFML and dependencies
这篇关于将sfml与cmake链接(Windows MinGW)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!