Setup CMake with SFML in VS2017

寵の児 提交于 2020-12-05 20:37:18

问题


Just like in CLion I want to use SFML with Visual Studio 2017, but I'm still learning cmake and I don't know the commands or the logic of how cmake works at all. I've just seen some posts and got this litle script.

Note: I downloaded the latest version of sfml in the link provided, I just taked the extrated directory and put alongside CMakeLists.txt in my folder

#sets up the minimum version of cmake
cmake_minimum_required(VERSION 3.9)

#how the project will be called
project (space_impact)

#set c++11 standard
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" -std=c++11)

#set source files
set (SOURCE_FILES main.cpp)

#we add the executable of the program
add_executable (space_impact ${SOURCE_FILES})

#taked from a mac-clion tutorial, doesn't work
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/SFML/cmake-modules/")
find_package (SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(space_impact ${SFML_LIBRARIES})
endif()

that thing gave me errors:

Error       CMake Error at SFML/cmake-modules/FindSFML.cmake:355 (message):
  Could NOT find SFML (missing: SFML_SYSTEM_LIBRARY SFML_WINDOW_LIBRARY
  SFML_GRAPHICS_LIBRARY SFML_NETWORK_LIBRARY SFML_AUDIO_LIBRARY)        SFML/cmake-modules/FindSFML.cmake       

I want everything to be dynamic, but I don't know how can I do that..
So my question is what should I do for setting up correctly SFML with Cmake in Visual Studio.
I don't want the old-fashioned method from the official website

UPDATE
Here's my location....

The thing is.. the FindSFML.cmake script it's not working...
What files should I move for make it working?


回答1:


Your script is perfectly fine, except three things I'd change:

  • Move the whole module detection before defining targets. I'm pretty sure you also have to define your include directories before.

  • Your if(SFML_FOUND) bracket is pretty pointless right now, because you've set SFML to be required, which means it will never get past find_package() unless it's found.

  • -std=c++11 is a GCC only flag (MSVC will always use the latest standard, unless specified). As such you'll have to check the compiler here or use CMAKE_CXX_STANDARD.

So the "cleaned" CMakeLists.txt could look like this:

#sets up the minimum version of cmake
cmake_minimum_required(VERSION 3.9) # personally I'd set this version as low as required; you don't have to require the cutting edge version

#how the project will be called
project (space_impact)

#set the C++ standard to be used
set (CMAKE_CXX_STANDARD 11)

#set source files
set (SOURCE_FILES main.cpp)

#look for SFML and add it
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/SFML/cmake-modules/")
find_package (SFML REQUIRED system window graphics network audio)

include_directories(${SFML_INCLUDE_DIR})

#we add the executable of the program
add_executable (space_impact ${SOURCE_FILES})
target_link_libraries(space_impact ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})

Note that adding SFML_DEPENDENCIES to the library list is optional, unless you're using a static version of SFML.

But what about your SFML issue? Since you don't have the SFML files installed in any directory looked into by default, you'll have to tell CMake where it's found using the CMake variable SFML_ROOT or the environment variable of the same name.

So the first time you're invoking CMake, it could look like this:

cmake -G "Visual Studio 15 2017" -DSFML_ROOT=path/to/sfml path/to/source



回答2:


This is all you need to compile sfml in your cmake project.

find_package(SFML 2.5.1 COMPONENTS system graphics audio network REQUIRED)
add_executable (AwesomeProject "AwesomeProject.cpp" "AwesomeProject.h")
target_link_libraries(AwesomeProject PRIVATE sfml-audio sfml-graphics sfml-network sfml-system)

Also set SFML_DIR var to your sfml folder.



来源:https://stackoverflow.com/questions/47406388/setup-cmake-with-sfml-in-vs2017

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