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

Author Topic: CMake in SFML 2.5  (Read 8679 times)

0 Members and 1 Guest are viewing this topic.

need_help_with_cmake

  • Newbie
  • *
  • Posts: 1
    • View Profile
CMake in SFML 2.5
« on: May 16, 2018, 04:59:53 am »
Hi SFML team and forum readers,

I'm trying to use the new SFML CMake files with minimal success. I have good knowledge of C++, but only working knowledge of CMake. I have a few goals I'm trying to accomplish:

1. Embed SFML in my project (gets compiled as part of project; no separate step)
2. Include SFML statically
3. Expose SFML to other packages (should be a result of 1)

I was able to get the default SFML project building so long as I don't change anything.

However, I want to include SFML in my project, and my current workflow is to call CMake twice - once for SFML, and once for my project. I would like to be able to set up my project such that SFML appears as part of the resultant solution/etc.

My project is set up as follows:

/cmake
|    /modules
|    |    Findimgui.cmake
|    |    Findimgui-sfml.cmake
|    CMakeLists.txt (Main project CMake file)
/intermediate
|    /SFML
|    SFMLConfig.cmake
|    SFMLConfigDependencies.cmake
|    ... etc
/third-party
|    /SFML
|    < basically contents of the SFML GitHub repo; some files/folders get deleted >
/my-project
|     ...

I have a Python script to run CMake, the relevant lines are as follows:
run("cmake -A x64 -T host=x64 -DBUILD_SHARED_LIBS=OFF ../../third-party/SFML/", cwd='intermediate/SFML/')
run("cmake -A x64 -T host=x64 -DBUILD_SHARED_LIBS=OFF ../cmake/", cwd='intermediate/')

My main CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)

project(my_proj)

set(my_proj_VERSION_MAJOR 0)
set(my_proj_VERSION_MINOR 1)

set(EXECUTABLE_NAME "my_proj")
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)

# Custom packages
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/modules/")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/SFML/cmake/Modules")

# SFML
#set(SFML_STATIC_LIBRARIES TRUE) # Can't compile static yet
set(SFML_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../intermediate/SFML/")
set(SFML_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/SFML/")
#set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/SFML/cmake/Modules")
#set(SFML_ROOT ${SFML_ROOT} "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/SFML/cmake/Modules")
#message(STATUS "CMAKE_LIBRARY_PATH: ${CMAKE_LIBRARY_PATH}")
find_package(SFML 2.5 COMPONENTS graphics audio network REQUIRED)

# Imgui
find_package(imgui)
include_directories(${IMGUI_INCLUDE_DIRS})

find_package(imgui-sfml)
include_directories(${IMGUI_SFML_INCLUDE_DIRS})

add_executable(my_proj ../main.cpp)

target_link_libraries(my_proj
    sfml-graphics
    sfml-audio
    sfml-network
    imgui
    imgui-sfml
    )

add_dependencies(my_proj
    imgui
    imgui-sfml
)

If I compile SFML as a shared library, I can get pretty close - but my_proj and imgui-sfml are trying to use .lib files instead of .dll files, so I get linker errors.

Contents of Findimgui-sfml.cmake:
cmake_minimum_required(VERSION 3.1)

project(imgui-sfml)

file(GLOB_RECURSE IMGUI_SFML_SRC
    "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/imgui-sfml/*.cpp"
)

find_package(SFML 2.5 COMPONENTS graphics REQUIRED)

set(IMGUI_SFML_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../third-party/imgui-sfml/")

add_library(imgui-sfml STATIC
    ${IMGUI_SFML_SRC}
)

target_link_libraries(imgui-sfml
    sfml-graphics
)

If I try to compile SFML statically, I get errors about missing packages:
-- Selecting Windows SDK version 10.0.17134.0 to target Windows 10.0.16299.
CMake Error at D:/code/my_proj/intermediate/SFML/SFMLConfig.cmake:139 (message):
  SFML found but some of its dependencies are missing ( FreeType OpenAL Ogg
  Vorbis VorbisFile VorbisEnc FLAC)
Call Stack (most recent call first):
  CMakeLists.txt:28 (find_package)


CMake Error at CMakeLists.txt:28 (find_package):
  Found package configuration file:

    D:/code/my_proj/cmake/../intermediate/SFML/SFMLConfig.cmake

  but it set SFML_FOUND to FALSE so package "SFML" is considered to be NOT
  FOUND.


-- Configuring incomplete, errors occurred!

I would appreciate any help/suggestions with this.

Thanks for your time, and please let me know if more information is required.

Thanks!
« Last Edit: May 16, 2018, 05:20:33 am by need_help_with_cmake »

Arxcis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: CMake in SFML 2.5
« Reply #1 on: June 08, 2018, 10:16:29 am »
I think I have a similar issue. I am trying to build SFML 2.5 statically on macOs in a single step,
together with the rest of my project.

This is how i want it to work.
git clone <myrepo>
mkdir build
cd build
cmake ..
make
./tutorialsfml
 


Here is the current output from my cmake ..
$ cmake ..
BINDIR /Users/<me>/Desktop/tutorial-sfmlcmake/build
SRCDIR /Users/<me>/Desktop/tutorial-sfmlcmake
CMake Error at build/lib/SFML-2.5.0/SFMLConfig.cmake:139 (message):
  Requested SFML configuration (Static) was not found
Call Stack (most recent call first):
  CMakeLists.txt:38 (find_package)


CMake Error at CMakeLists.txt:38 (find_package):
  Found package configuration file:

    /Users/<me>/Desktop/tutorial-sfmlcmake/build/lib/SFML-2.5.0/SFMLConfig.cmake

  but it set SFML_FOUND to FALSE so package "SFML" is considered to be NOT
  FOUND.


-- Configuring incomplete, errors occurred!
See also "/Users/<me>/Desktop/tutorial-sfmlcmake/build/CMakeFiles/CMakeOutput.log".
 

Here is my project CmakeLists.txt
#
# Relevant command line options
# Passed like this: "cmake .. -D<key1>=<value1> -D<key2>=<value2> ..."
#
# Key                 Value
# --------------------------------
# CMAKE_BUILD_TYPE    Debug | RelWithDebInfo | Release
# CMAKE_CXX_COMPILER  <path to compiler>
# CMAKE_CXX_STDLIB    <path to standardlib>
#
project(tutorialsfml)
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)

set(SRCDIR ${CMAKE_SOURCE_DIR})
set(BINDIR ${CMAKE_BINARY_DIR})

link_directories(${CMAKE_CXX_STDLIB}) # For clang 6.0 on macOs

message("BINDIR" ${BINDIR})
message("SRCDIR" ${SRCDIR})

#
# SFML
#
add_subdirectory(${SRCDIR}/lib/SFML-2.5.0)
set(SFML_DIR ${BINDIR}/lib/SFML-2.5.0)
set(CMAKE_FIND_FRAMEWORK "NEVER") # for macOs
set(SFML_STATIC_LIBRARIES TRUE)
find_package (SFML 2.5 REQUIRED graphics window system)

# Create target executeable
add_executable(tutorialsfml ${SRCDIR}/src/main.cpp)
target_link_libraries(
    tutorialsfml
    PRIVATE sfml-graphics sfml-window sfml-system)

 
« Last Edit: June 08, 2018, 10:21:30 am by Arxcis »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: CMake in SFML 2.5
« Reply #2 on: June 08, 2018, 10:22:45 am »
I'm not sure as I'm no macOS user, but I think we aren't shipping static libraries for macOS.
The recommended way to work on macOS is to build bundles that includes everything from executable to dylibs to resources.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Arxcis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: CMake in SFML 2.5
« Reply #3 on: June 08, 2018, 11:33:42 am »
When I turn off static build, building SFML works, but it is still a two step process. Like OP I want to be able to build SFML as part of my project in a single make call

Reiterating what OP said:
 "...my current workflow is to call CMake twice - once for SFML, and once for my project. I would like to be able to set up my project such that SFML appears as part of the resultant solution/etc."

Arxcis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: CMake in SFML 2.5
« Reply #4 on: June 08, 2018, 11:49:04 am »
So far I have tried using add_subdirectory() to generate the SFMLCmake.config file, and then using find_package(). (This looks very hacky to me...)

add_subdirectory(${SRCDIR}/lib/SFML-2.5.0)
set(SFML_DIR ${BINDIR}/lib/SFML-2.5.0)
find_package (SFML 2.5 REQUIRED window graphics system)
 

Cmake runs fine and SFML builds, but i still get linker errors:

<me> in ~/Desktop/tutorial-sfmlcmake/build
$ make
BINDIR/Users/<me>/Desktop/tutorial-sfmlcmake/build
SRCDIR/Users/<me>/Desktop/tutorial-sfmlcmake
-- Found SFML 2.5.0 in /Users/<me>/Desktop/tutorial-sfmlcmake/build/lib/SFML-2.5.0
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/<me>/Desktop/tutorial-sfmlcmake/build
[ 13%] Built target sfml-system
[ 46%] Built target sfml-window
[ 72%] Built target sfml-graphics
[ 73%] Linking CXX executable tutorialsfml
Undefined symbols for architecture x86_64:
  "sf::String::String(char const*, std::locale const&)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [tutorialsfml] Error 1
make[1]: *** [CMakeFiles/tutorialsfml.dir/all] Error 2
make: *** [all] Error 2

 
« Last Edit: June 08, 2018, 11:53:26 am by Arxcis »

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: CMake in SFML 2.5
« Reply #5 on: June 10, 2018, 01:12:20 pm »
I'm not sure as I'm no macOS user, but I think we aren't shipping static libraries for macOS.
The recommended way to work on macOS is to build bundles that includes everything from executable to dylibs to resources.

This is correct (both about static lib and bundle applications).

@Arxcis: building SFML is one thing, linking your project against SFML is another. Make sure you've done the latter as well.
SFML / OS X developer

 

anything