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

Author Topic: Integrate SFML as a git submodule  (Read 2785 times)

0 Members and 1 Guest are viewing this topic.

vono

  • Newbie
  • *
  • Posts: 3
    • View Profile
Integrate SFML as a git submodule
« on: October 09, 2019, 07:34:30 pm »
Hi,
I'd like to add SFML as a git submodule to an existing CMake project. Let's take this CMakeLists.txt as the starting point:
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project("Test")

set(SOURCE_FILES
        src/main.cpp
)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})
 

To add SFML as a submodule in the directory ./libs/sfml/, I do:
git submodule add https://github.com/SFML/SFML.git libs/sfml
 

And then I adjust my CMakeLists.txt to:
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project("Test")

set(SOURCE_FILES
        src/main.cpp
)

set(BUILD_SHARED_LIBS FALSE)
add_subdirectory(libs/sfml)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

target_link_libraries(${PROJECT_NAME} sfml-graphics)
target_link_libraries(${PROJECT_NAME} sfml-audio)
 


It works, but is this the proper way to integrate SFML as a git submodule (statically linked)? I'm not sure if it's bad practice or if it's the way to go as I bypass the existing SFMLConfig.cmake script / the find_package(SFML ...) macro.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Integrate SFML as a git submodule
« Reply #1 on: October 09, 2019, 11:54:56 pm »
I would actually find SFML with find_package(), it automatically sets SFML up.

Here is an example of finding SFML in my library Thor:
https://github.com/Bromeon/Thor/blob/master/CMakeLists.txt#L110-L120

Set the SFML_DIR variable to the folder of your git submodule. It's noteworthy that SFML had a breaking change in the way it's linked in SFML 2.5, you can see the adaptation from a client (Thor in this case) in this commit:
https://github.com/Bromeon/Thor/commit/9af355ab27f77fea912bced1b37f0f413a41ff5b
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vono

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Integrate SFML as a git submodule
« Reply #2 on: October 10, 2019, 12:06:19 pm »
Hi Nexus,
thank you for your reply. I've already tried this before starting this thread, the CMakeLists.txt looks like the following:
cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
project("Test")

set(SOURCE_FILES
        src/main.cpp
)

set(SFML_STATIC_LIBRARIES TRUE)
set(SFML_DIR "libs/sfml/")
find_package(SFML 2.5 COMPONENTS audio graphics window system REQUIRED)

add_executable(${PROJECT_NAME} ${SOURCE_FILES})

target_link_libraries(${PROJECT_NAME} sfml-graphics)
target_link_libraries(${PROJECT_NAME} sfml-audio)
 

But the problem is (at least I think so) that find_package does not trigger any build process, so the SFMLConfig.cmake is not created from the SFMLConfig.cmake.in.

I should clarify that I want to integrate the SFML build process into my project. I don't like to build SFML manually in it's subfolder before I can start building my main project. I'd like to clone my project's repository, including SFML as a submodule, hit the 'build' button once and everything should be build automatically (First SFML, then my project).

As far as I understand, find_package only works if SFML was build beforehand (or at least the SFMLConfig.cmake has to be created). But I could also be absolutely wrong, unfortunately I didn't do that much with CMake yet.

To make this post complete, here's the CMake error message using the above posted CMakeLists.txt:
CMake Error at CMakeLists.txt:12 (find_package):
  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.
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Integrate SFML as a git submodule
« Reply #3 on: October 10, 2019, 12:53:04 pm »
Of course find_package is for finding an existing installation of SFML. The way you did it initially was right for building SFML as part of your project.

Quote
It works, but is this the proper way to integrate SFML as a git submodule (statically linked)? I'm not sure if it's bad practice or if it's the way to go as I bypass the existing SFMLConfig.cmake script / the find_package(SFML ...) macro.
Yes, this is the proper way. Hard to find a simpler way to do it ;)
You're not bypassing anything, since all the stuff you mention is for using an already compiled/installed SFML.
Laurent Gomila - SFML developer

vono

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Integrate SFML as a git submodule
« Reply #4 on: October 10, 2019, 02:17:57 pm »
Thank you!