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

Author Topic: How to integrate SFML's dependencies into my CMake project?  (Read 12654 times)

0 Members and 1 Guest are viewing this topic.

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
How to integrate SFML's dependencies into my CMake project?
« on: September 14, 2014, 02:35:30 pm »
I use MinGW32 and want to link to SFML statically. This is are the steps I have done.
  • Download official SFML release for MinGW and copied it to D:\lib\SFML-2.1.
  • Set up a CMake project according to this guide.
  • Added set(SFML_ROOT D:/lib/SFML-2.1) to my CMakeLists.txt.
At this step, it compiles and runs fine when I copy all SFML DLLs in the same folder as my executable. However, I want static linking. Therefore, I also added set(SFML_STATIC_LIBRARIES ON) to my CMakeLists.txt. This causes the following error:

Error: SFML found but some of its dependencies are missing ( FreeType GLEW libjpeg OpenAL libsndfile)

So I have to link to all those dependencies myself. On Windows, it is painful to compile them myself. Because of that, it's tempting to use the already compiled dependencies from the SFML repository.

What is the best way to integrate those libraries into my CMake project?
« Last Edit: September 14, 2014, 03:46:56 pm by sharethis »

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #1 on: September 15, 2014, 03:07:46 am »
Can someone experienced with CMake please give me an advice on how to handle SFML's dependencies? I already tried to build all of them myself, but CMake linked just the *.dll.a files. Moreover, it seems like building them is not needed since binaries are included in the SFML repository. Also it would be easier for other developers on Windows to just install SFML instead all of it's dependencies, too.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: How to integrate SFML's dependencies into my CMake project?
« Reply #2 on: September 15, 2014, 06:27:46 am »
SFML ships all thd dependencies on Wibdows for a reason, i.e. so you don't havr to build them yourself. For OpenAL it's actually "required" that you use the provided version, since there can be strange crashes with a different one.

For linking the dependecies via CMake you could look at SFML's example projects, they link SFML. Or you could look at Thor's examples, they use SFML as well.
If I recall right, when you tell the FindSFML.cmake to search the static SFML libs, it will add the SFML dependencies to a CMake variable - just take a look on how FindSFML.cmake has been implemented.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #3 on: September 15, 2014, 10:56:13 am »
SFML ships all thd dependencies on Wibdows for a reason, i.e. so you don't havr to build them yourself.

Is there a reason that the extlibs are not shipped with the precompiled versions of SFML that you can download from the website, but only with the repository? So I think I have to either build SFML or build it's dependencies. Or I have to download both, the release archive and the repository and copy them together into the same directory?

For linking the dependecies via CMake you could look at SFML's example projects, they link SFML. Or you could look at Thor's examples, they use SFML as well.
If I recall right, when you tell the FindSFML.cmake to search the static SFML libs, it will add the SFML dependencies to a CMake variable - just take a look on how FindSFML.cmake has been implemented.

But thanks for pointing out Thor, it looks clear and is a good reference. There is a variable SFML_DEPENDENCIES that contain a list of the libraries. That's great. I just have to make SFML find the libraries now.
« Last Edit: September 15, 2014, 11:04:07 am by sharethis »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #4 on: September 15, 2014, 11:14:49 am »
Is there a reason that the extlibs are not shipped with the precompiled versions of SFML that you can download from the website, but only with the repository? So I think I have to either build SFML or build it's dependencies. Or I have to download both, the release archive and the repository and copy them together into the same directory?
In the past (SFML 2.1 and older), SFML included the dependencies directly in its statics libraries. This got changed for the up coming SFML 2.2, you now need to link against all dependencies with static libraries. So in the past there was not reason to ship the dependencies.
At best you download the SFML master and build the INSTALL project, that will also install the right extlibs when building statically.

But thanks for pointing out Thor, it looks clear and is a good reference. There is a variable SFML_DEPENDENCIES that contain a list of the libraries. That's great. I just have to make SFML find the libraries now.
You can simply use find_package() in CMake, just look again at the Thor's CMake script. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #5 on: September 15, 2014, 02:45:30 pm »
Thanks for your reply, I got a bit further. CMake finds all dependencies now. However, I get compiler errors now. These are the steps I performed.

1. Download a snapshot of SFML from Github and extract it.
2. Started CMake GUI and selected the exctracted folder as both source and destination.
3. Clicked "Configure" and selected "MinGW Makefiles".
4. Unchecked "BUILD_SHARED_LIBS"
    Set "CMAKE_INSTALL_PREFIX" to "D:/lib/SFML"
    Set "GLEW_INCLUDE_PATH" to "D:/lib/glew-1.11.0/include"
    Set "GLEW_LIBRARY" to "D:/lib/glew-1.11.0/lib/libglew32.a"
    Checked "SFML_USE_STATIC_STD_LIBS"
5. Clicked "Configure" again, and then "Generate".
6. Ran "mingw32-make install" inside the extracted folder.

However, I get these compiler errors with the CMakeLists.txt from the tutorial and the FindSFML.cmake from the official repository. I took the example code from the compilation tutorial.

"D:\program\JetBrains\CLion 138.1965.18\bin\cmake\bin\cmake.exe" --build C:\Users\main\.clion10\system\cmake\generated\dff727e\dff727e\Debug --target test -- -j 6
Linking CXX executable test.exe
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x124): undefined reference to `sf::Clock::Clock()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x129): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x12f): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x461): undefined reference to `sf::err()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x54c): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x552): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x57e): undefined reference to `sf::seconds(float)'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x5f4): undefined reference to `sf::err()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x6f6): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x6fc): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x715): undefined reference to `sf::Clock::restart()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x75b): undefined reference to `sf::Clock::Clock()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x760): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x769): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x7d4): undefined reference to `sf::err()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x8e1): undefined reference to `sf::err()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x9b4): undefined reference to `sf::Clock::Clock()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x9bc): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0x9c2): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xa9e): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xaa4): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xabe): undefined reference to `sf::operator!=(sf::Time, sf::Time)'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xacc): undefined reference to `sf::Clock::getElapsedTime() const'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xae6): undefined reference to `sf::operator-(sf::Time, sf::Time)'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xaf2): undefined reference to `sf::sleep(sf::Time)'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xaf9): undefined reference to `sf::Clock::restart()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xb06): undefined reference to `sf::err()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xc56): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xc5c): undefined reference to `sf::Time::Zero'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xc75): undefined reference to `sf::Clock::restart()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xd82): undefined reference to `sf::err()'
D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj):Window.cpp:(.text+0xe94): undefined reference to `sf::err()'
d:/program/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: D:\lib\SFML\lib\libsfml-window-s.a(Window.cpp.obj): bad reloc address 0x17 in section `.text.unlikely'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\test.dir\build.make:96: recipe for target 'test.exe' failed
mingw32-make.exe[3]: *** [test.exe] Error 1
CMakeFiles\Makefile2:59: recipe for target 'CMakeFiles/test.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/test.dir/all] Error 2
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/test.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/test.dir/rule] Error 2
Makefile:105: recipe for target 'test' failed
mingw32-make.exe: *** [test] Error 2
« Last Edit: September 15, 2014, 11:05:21 pm by sharethis »

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #6 on: September 15, 2014, 11:21:08 pm »
Is it possible that the SFML built has failed to some extend? I don't understand the error messages though.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #7 on: September 15, 2014, 11:38:41 pm »
By the looks of it, you're not linking against sfml-system...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #8 on: September 15, 2014, 11:43:37 pm »
By the looks of it, you're not linking against sfml-system...

This is my CMakeLists.txt. It runs without any warnings. Moreover, "libsfml-system-s.a" is available under "D:\lib\SFML\lib". But I think, when I built SFML with "mingw32-make install", there showed up some red lines in the terminal. Maybe something is wrong with my SFML installation. Do you have any idea how I can debug the issue further?

# General settings
cmake_minimum_required(VERSION 2.8.4)
set(PROJECT_NAME test)
project(${PROJECT_NAME})

# Enable debug symbols by default
if (CMAKE_BUILD_TYPE STREQUAL "")
        set(CMAKE_BUILD_TYPE Debug)
endif()

# Define sources and executable
set(EXECUTABLE_NAME ${PROJECT_NAME})
set(SOURCE_FILES main.cpp)
add_executable(${EXECUTABLE_NAME} ${SOURCE_FILES})

# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
set(SFML_ROOT "" CACHE FILEPATH "Path to SFML library.")
set(SFML_STATIC_LIBRARIES ON)
find_package(SFML 2 REQUIRED system window graphics network audio)
if (SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
  target_link_libraries(${EXECUTABLE_NAME} ${SFML_DEPENDENCIES})
else()
        message(SEND_ERROR "SFML library not found. Please set SFML_ROOT.")
endif()
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #9 on: September 15, 2014, 11:50:15 pm »
I'm not sure if it matters in the CMake file, but what if you replaced the existing line with this one:
find_package(SFML 2 REQUIRED graphics window network audio system)
When linking via command line, it would require the right order (graphics depends on window, and they all depend on system).

The "linking library XYZ" is usually colored in red, when building with MinGW, but if you want to check out the errors, you can simply run the following command and then look at the log2.txt file.
mingw32-make install 1> log1.txt 2> log2.txt
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sharethis

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #10 on: September 16, 2014, 12:07:33 am »
Wow, thanks a lot. The order in the find_package command really did the trick! I'll update the wiki so that others don't have to struggle with it.

cpainterwakefield

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to integrate SFML's dependencies into my CMake project?
« Reply #11 on: September 17, 2014, 01:56:47 am »

In the past (SFML 2.1 and older), SFML included the dependencies directly in its statics libraries. This got changed for the up coming SFML 2.2, you now need to link against all dependencies with static libraries. So in the past there was not reason to ship the dependencies.
At best you download the SFML master and build the INSTALL project, that will also install the right extlibs when building statically.


Just curious; why the change?  We are looking at using SFML in a course setting, and having to link in yet more libraries complicates things for our students (who are very new to building software in C++).  Is there an easy way to modify the SFML distro to link everything into one library statically (excepting the openal32 and libsndfile)?

UPDATE:  Never mind - I found the info here: https://github.com/SFML/SFML/wiki/FAQ#wiki-build-link-static
« Last Edit: September 17, 2014, 02:17:55 am by cpainterwakefield »

 

anything