linking sfml with cmake (Windows MinGW)

允我心安 提交于 2020-06-27 17:49:27

问题


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})

Error I get when running 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)

Call Stack (most recent call first): CMakeLists.txt:9 (find_package)

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..

回答1:


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:

  • Move the FindSFML.cmake script to a sub directory of your own project, e.g. cmake/FindSFML.cmake and adjust the CMAKE_MODULE_PATH value accordingly.
  • Add a new CMake variable SFML_ROOT pointing to the directory where you installed SFML (in your case C:/Libraries/sfml). This shouldn't be hardcoded in the CMakeLists.txt file and instead be passed once when invoking CMake (i.e. cmake -DSFML_ROOT=C:/...; this is saved in the cache).

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


来源:https://stackoverflow.com/questions/45671299/linking-sfml-with-cmake-windows-mingw

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!