简体   繁体   English

将sfml与cmake链接(Windows MinGW)

[英]linking sfml with cmake (Windows MinGW)

I can't seem to link SFML to my executable using cmake . 我似乎无法使用cmakeSFML链接到我的可执行文件。

CMakeLists.txt: 的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})

Error I get when running cmake: 运行cmake时出现错误:

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) C:/Libraries/sfml/cmake/Modules/FindSFML.cmake中的CMake错误:355(消息):找不到SFML(缺少:SFML_SYSTEM_LIBRARY SFML_WINDOW_LIBRARY SFML_GRAPHICS_LIBRARY)

Call Stack (most recent call first): CMakeLists.txt:9 (find_package) 调用堆栈(最近的调用优先):CMakeLists.txt:9(find_package)

the sfml directory contains a 32bit MinGW compiled sfml repository. sfml目录包含一个32位MinGW编译的sfml存储库。 I am using Windows. 我正在使用Windows。 The cmake command I use is: 我使用的cmake命令是:

cmake -G "MinGW Makefiles" ..dir..

The module to look for SFML won't look relative to its own position. 寻找SFML的模块与它自己的位置无关。 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. 取而代之的是,它将尝试一些常见的路径(非Windows系统)以及一些尝试查找实际库的特定变量。

To solve this, you should do two things: 要解决此问题,您应该做两件事:

  • Move the FindSFML.cmake script to a sub directory of your own project, eg cmake/FindSFML.cmake and adjust the CMAKE_MODULE_PATH value accordingly. FindSFML.cmake脚本移动到您自己的项目的子目录中,例如cmake/FindSFML.cmake并相应地调整CMAKE_MODULE_PATH值。
  • Add a new CMake variable SFML_ROOT pointing to the directory where you installed SFML (in your case C:/Libraries/sfml ). 添加一个新的CMake变量SFML_ROOT指向安装SFML的目录(在本例中为C:/Libraries/sfml )。 This shouldn't be hardcoded in the CMakeLists.txt file and instead be passed once when invoking CMake (ie cmake -DSFML_ROOT=C:/... ; this is saved in the cache). 不应在CMakeLists.txt文件中对此进行硬编码,而应在调用CMake时将其传递一次(即cmake -DSFML_ROOT=C:/... ;这将保存在缓存中)。

Also there are a few problems with the structure of your CMakeLists.txt . 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM