问题
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 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).
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