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

Author Topic: SFML, ImGui and SFML-ImGui with CMake  (Read 2464 times)

0 Members and 1 Guest are viewing this topic.

MypkaXD

  • Newbie
  • *
  • Posts: 1
    • View Profile
SFML, ImGui and SFML-ImGui with CMake
« on: September 22, 2023, 04:26:39 pm »
Hello! I have some problems with linking ImGui, SFML and SFML-ImGui for my project.
My project contain main CMakeLists.txt, .gitsubmodule, folders: include for including headers which i wrote, source for cpp-files and libs (Photo: SFML1). Libs are contain SFML, ImGui, SFML-ImGui folders and second CMakeLists.txt (Photo SFML2).
Main CMakeLists.txt:
cmake_minimum_required(VERSION 3.2) # Min version CMake

set(CMAKE_CXX_STANDARD 17)            
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(Dynamic_2
        VERSION 1.0
        LANGUAGES C CXX
)

add_executable(Dynamic_2 ${CMAKE_CURRENT_SOURCE_DIR}/source/Dynamic_2.cpp)

target_include_directories(
  Dynamic_2                          
  PUBLIC                              
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

add_subdirectory(${CMAKE_SOURCE_DIR}/libs)

target_link_libraries(Dynamic_2 PUBLIC Libs)
 
CMakeLists.txt from libs:
# ImGui
add_library(Libs STATIC
        ${CMAKE_SOURCE_DIR}/libs/ImGui/imgui.cpp
        ${CMAKE_SOURCE_DIR}/libs/ImGui/imgui_demo.cpp
        ${CMAKE_SOURCE_DIR}/libs/ImGui/imgui_draw.cpp
        ${CMAKE_SOURCE_DIR}/libs/ImGui/imgui_tables.cpp
        ${CMAKE_SOURCE_DIR}/libs/ImGui/imgui_widgets.cpp
)
target_include_directories(Libs PUBLIC ${CMAKE_SOURCE_DIR}/libs/ImGui)

# SFML
set(CMAKE_BUILD_TYPE "RelWithDebInfo")
set(SFML_USE_STATIC_STD_LIBS ON)
set(SFML_BUILD_NETWORK ON)
set(SFML_BUILD_WINDOW ON)
set(SFML_BUILD_GRAPHICS ON)
set(SFML_BUILD_AUDIO ON)
set(SFML_BUILD_DOC OFF)
set(SFML_BUILD_EXAMPLES OFF)
set(SFML_DIR ${CMAKE_CURRENT_SOURCE_DIR}/SFML)

add_subdirectory(${CMAKE_SOURCE_DIR}/libs/SFML)

# find_package(SFML 2.6.0 COMPONENTS network window graphics audio)

target_include_directories(Libs PUBLIC ${CMAKE_SOURCE_DIR}/libs/SFML/include)
target_link_libraries(Libs sfml-network sfml-window sfml-graphics sfml-audio)

set(IMGUI_SFML_FIND_SFML OFF)
set(IMGUI_SFML_IMGUI_DEMO ON)
set(IMGUI_DIR ${CMAKE_SOURCE_DIR}/libs/ImGui)
set(SFML_DIR ${CMAKE_SOURCE_DIR}/libs/SFML/cmake)

add_subdirectory(${CMAKE_SOURCE_DIR}/libs/ImGui-SFML)

#target_sources(Libs PUBLIC
#        ${CMAKE_SOURCE_DIR}/libs/ImGui-SFML/imgui-SFML.cpp
#)

target_include_directories(Libs PUBLIC ${CMAKE_SOURCE_DIR}/libs/ImGui-SFML)
 
So it's working. I can build this. But!
When i compiled this i got some mistakes (Photo Mistakes)
And i don't know how to fix it. I looked many articles about it but can't to solve.
Libs which i'm using:
1)Latest ImGui: https://github.com/ocornut/imgui/tree/d6360c1ba9934e18ea46913a9e6f3f888a2521b8
2) Latest ImGuiSFML: https://github.com/SFML/imgui-sfml/tree/6bd43671d3fab76f94b1f0877d943d8cc3b34c3b
3) Latest SFML: https://github.com/SFML/SFML/tree/ff5eaca6a2219a5c172030df27b56b4d78f6ee91

If someone helps me I will be glad.

Now i'm using other version of libs and it's working (found this way):
New CMakeLists.txt from folder libs:
include(FetchContent)

set(SFML_VERSION 2.5.1)
set(IMGUI_VERSION 1.87)
# set(IMGUI_SFML_VERSION 2.3)

# It's nice to get stripped-down release zips instead of cloning
# a repo and checking out a tag
FetchContent_Declare(
  SFML
  URL "https://github.com/SFML/SFML/archive/${SFML_VERSION}.zip"
)

FetchContent_Declare(
  imgui
  URL "https://github.com/ocornut/imgui/archive/v${IMGUI_VERSION}.zip"
)

# Or by tag...
FetchContent_Declare(
  imgui-sfml
  GIT_REPOSITORY https://github.com/SFML/imgui-sfml.git
  GIT_TAG        2.6.x
  # GIT_COMMIT 5f54b69b6aee18db846c81633f86f78c2586dded
  # ^ or like this - sometimes it's better because FetchContent won't look
  # into remote to see if branch head was updated or not - good for stable
  # tags like 'vX.X' corresponding to releases
)

## finally, let's build

# SFML
option(SFML_BUILD_AUDIO "Build audio" OFF)
option(SFML_BUILD_NETWORK "Build network" OFF)
FetchContent_MakeAvailable(sfml)

# Dear ImGui
FetchContent_MakeAvailable(imgui)

# ImGui-SFML
set(IMGUI_DIR ${imgui_SOURCE_DIR})
option(IMGUI_SFML_FIND_SFML "Use find_package to find SFML" OFF)
option(IMGUI_SFML_IMGUI_DEMO "Build imgui_demo.cpp" ON)

FetchContent_MakeAvailable(imgui-sfml)
New CMakeLists.txt Main:
cmake_minimum_required(VERSION 3.2) # Min version CMake

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(Dynamic_2
        VERSION 1.0
        LANGUAGES C CXX
)

add_executable(Dynamic_2 ${CMAKE_CURRENT_SOURCE_DIR}/source/Dynamic_2.cpp)

target_include_directories(
  Dynamic_2
  PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/include
)

set(BUILD_SHARED_LIBS OFF)

add_subdirectory(${CMAKE_SOURCE_DIR}/libs)

target_link_libraries(Dynamic_2
  PUBLIC
    ImGui-SFML::ImGui-SFML
)

I want to understand why my way isn't working. Sorry if i made mistakes (english not my native language) and for design mistakes.

Thrasher

  • SFML Team
  • Jr. Member
  • *****
  • Posts: 57
    • View Profile
Re: SFML, ImGui and SFML-ImGui with CMake
« Reply #1 on: September 22, 2023, 08:49:08 pm »
The new way you're doing it is much better so please keep using that solution. It's better in many ways than the old version.

I think your old solution was failing because you I don't see you ever compiling the ImGui-SFML sources into Libs.

For future reference, the official SFML CMake template project includes a branch that demonstrates how to use ImGui-SFML. Check it out here: https://github.com/SFML/cmake-sfml-project/blob/imgui-sfml/CMakeLists.txt