Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to build SFML as a third-part library with cmake?  (Read 4736 times)

0 Members and 1 Guest are viewing this topic.

mxwxz

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to build SFML as a third-part library with cmake?
« on: December 27, 2018, 05:12:00 pm »
Hi, I'm starting to try SFML, it works fine when I manually build(or use the SDK), and add it to the "Additional Library". But now I'd like to build a project with cmake and I want a "one click" solution to get it built.

Here is my folder:
root
--SFML(the src code from github)
--Myapp
----main.cpp
----main.h
----CMakeLists.txt
--CMakeLists.txt

In main.cpp and main.h is just a "Hello world" code of SFML, so we dont talk about it.
In "root/CMakeLists.txt":
cmake_minimum_required (VERSION 3.8)
project ("test")
add_subdirectory ("SFML")
add_subdirectory ("Myapp")

In "root/Myapp/CMakeLists.txt":
cmake_minimum_required (VERSION 3.8)

find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
add_executable (myapp "main.cpp" "main.h")
target_link_libraries(myapp sfml-graphics sfml-audio)

Just like the wiki, but I got an error in VS 2017:
Requested SFML configuration (Shared) was not found

I found that in "SFMLConfig.cmake" there is
set(targets_config_file "${CMAKE_CURRENT_LIST_DIR}/SFML${config_name}Targets.cmake")
And I cant find the "SFML${config_name}Targets.cmake" in the "CMAKE_CURRENT_LIST_DIR", so it break.

I also tried in ubuntu using
mkdir build
cd build
cmake ..
I got these:
  By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SFML", but
  CMake did not find one.

  Could not find a package configuration file provided by "SFML" (requested
  version 2.5) with any of the following names:

    SFMLConfig.cmake
    sfml-config.cmake

  Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or set
  "SFML_DIR" to a directory containing one of the above files.  If "SFML"
  provides a separate development package or SDK, be sure it has been
  installed.

Now I dont want to install the SDK manually everytime, how can i use just "cmake .." and it will build SFML and then build myapp linked with it? Any suggestions?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How to build SFML as a third-part library with cmake?
« Reply #1 on: December 27, 2018, 05:50:32 pm »
Don't find_package SFML if it's part of your build. CMake already knows the SFML targets since it manages them, so directly use them:

add_subdirectory("SFML")
...
target_link_libraries(myapp sfml-graphics sfml-audio)
Laurent Gomila - SFML developer

mxwxz

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to build SFML as a third-part library with cmake?
« Reply #2 on: December 28, 2018, 01:08:28 am »
Awesome! That works, thanks.

 

anything