首页 > 解决方案 > 如何使用 CMake 配置 TGUI + SFML

问题描述

我目前正在做一个项目,我决定将 TGUI 与 SFML 一起使用来创建菜单。SFML 与 CMake 配合得很好,我认为配置 TGUI 几乎是一回事,但我不确定......如何配置 TGUI?

我的代码现在看起来像这样:

#documentation for configuring SFML with CMake : https://en.sfml-dev.org/forums/index.php?topic=24070.0

cmake_minimum_required(VERSION 3.17.3)
set(CMAKE_CXX_STANDARD 14)

project(AlgorithmsSimulator)

#location of project's files
include_directories(${CMAKE_SOURCE_DIR})

#directory for excutables
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

#object files
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib)


#configuring SFML for windows
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
    set(WINDOWS TRUE)
    set(SFML_STATIC_LIBRARIES TRUE)
    set(SFML_DIR "./SFML_Win/lib/cmake/SFML")
    
#MacOS
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")           
    set(MACOSX TRUE)
    set(SFML_INCLUDE_DIR "./SFML_macOS/include")
    set(SFML_LIBRARY_DIR "./SFML_macOS/lib")
    link_directories(SFML_LIBRARY_DIR)
    include_directories(SFML_INCLUDE_DIR)
    
#Linux
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
    set(LINUX TRUE)
    #set(SFML_STATIC_LIBRARIES TRUE)
    set(SFML_DIR "./SFML_Linux/lib/cmake/SFML")
    
else()
    message(FATAL_ERROR "Your Operating System is not supported")
endif()

# TGUI configuration 
set(TGUI_DIR "./TGUI/lib/cmake/TGUI")

#after configuring the paths of sfml for windows, macOS and linux cmake needs only to find the below packages 
find_package(SFML 2.5 COMPONENTS audio graphics window system network REQUIRED)
find_package(TGUI 0.8 REQUIRED)


#compiling & list the header an source files in the solution explorer
set(HEADERS AlgorithmsImplementation.h)
set(SOURCES AlgorithmsImplementation.cpp Main.cpp)
add_executable(${PROJECT_NAME} ${HEADERS} ${SOURCES})

# sfml .dll files and the .dll files for tgui; when tgui-d is added I get this linking error: tgui-d.lib cannot be opened and when I exclude it, it says that it cannot found tgui-d.dll
target_link_libraries(${PROJECT_NAME} sfml-audio sfml-graphics sfml-window sfml-system sfml-network tgui tgui-d)


#this will allow us to install an .exe file of the project
install(TARGETS ${PROJECT_NAME} DESTINATION bin) #${CMAKE_INSTALL_PREFIX}/bin
install(FILES Main.cpp DESTINATION src)          #${CMAKE_INSTALL_PREFIX}/src

标签: c++visual-studiocmake

解决方案


推荐阅读