CMake cannot find SFML

Started by luka, 26 March 2024, 06:27:21

luka

Hello, I'm currently trying to integrate TGUI into my SFML project. I'm using CMake and VS Code, but I've encountered a problem where TGUI can't find SFML. I have the error message ready here, which mentions setting the SFML_DIR, but I'm not sure exactly where to do that. Any help would be greatly appreciated.

[main] Building folder: AutoBackupScript2
[main] Configuring project: AutoBackupScript2
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=C:\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\mingw64\bin\g++.exe -SC:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/TGUI -Bc:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake]
[cmake] Searching for SFML 2...
[cmake]
[cmake] -- Could NOT find SFML (missing: SFML_DIR)
[cmake]
[cmake] Searching for SFML 3...
[cmake]
[cmake] -- Could NOT find SFML (missing: SFML_DIR)
[cmake] CMake Error at cmake/Dependencies.cmake:80 (message):
[cmake]  CMake couldn't find SFML.
[cmake]
[cmake]  Set SFML_DIR to the directory containing SFMLConfig.cmake (usually
[cmake]  something like SFML_ROOT/lib/cmake/SFML)
[cmake]
[cmake] Call Stack (most recent call first):
[cmake]  cmake/Dependencies.cmake:93 (tgui_find_dependency_sfml)
[cmake]  src/Backend/CMakeLists.txt:153 (tgui_add_dependency_sfml)
[cmake]  src/CMakeLists.txt:305 (include)
[cmake]
[cmake]
[cmake] -- Configuring incomplete, errors occurred!
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=C:\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\mingw64\bin\g++.exe -SC:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/TGUI -Bc:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build -G "MinGW Makefiles" exited with code: 1


I have integrated TGUI into my project as follows:

Project/
├── CMakeLists.txt (Main project)
├── src/
│  └── main.cpp
└── TGUI/

and SFML is not directly in the project folder, but it usually finds it after setting the SFML_DIR.

My CMakeLists.txt from the main project:

cmake_minimum_required(VERSION 3.17)
project(AutoBackupScript VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)

# Füge die Quelldateien hinzu
set(SOURCES
    src/main.cpp

)

include_directories(headers)

# Füge das ausführbare Ziel hinzu und verlinke ImGui
add_executable(AutoBackupScript ${SOURCES})

#target_link_libraries(AutoBackupScript PRIVATE TGUI)

# Suche und verlinke SFML
set(SFML_DIR "c:/SFML/lib/cmake/SFML")  # Pfad zum SFML CMake-Modul
find_package(SFML COMPONENTS system window graphics network audio REQUIRED)
if (SFML_FOUND)
    target_link_libraries(AutoBackupScript PRIVATE sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
endif()

#target_link_libraries(AutoBackupScript PRIVATE TGUI)

set(SFML_DIR "c:/SFML/lib/cmake/SFML")
#include_directories(${CMAKE_SOURCE_DIR}/TGUI)
#include_directories(${CMAKE_SOURCE_DIR})
#add_subdirectory(TGUI)

#find_package(TGUI 1 REQUIRED)
#target_link_libraries(AutoBackupScript PRIVATE TGUI::TGUI)

# Kopiere DLLs in das Build-Verzeichnis
if(WIN32)
    file(GLOB BINARY_DEP_DLLS "c:/SFML/bin/*.dll")
    file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
    file(GLOB MINGW_DEP_DLLS "C:/mingw64/bin/*.dll")
    file(COPY ${MINGW_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
endif()


texus

The set(SFML_DIR "c:/SFML/lib/cmake/SFML") should be sufficient when placed before the find_package or add_subdirectory line.

TGUI tries to find SFML with "find_package(SFML 2 CONFIG COMPONENTS graphics REQUIRED)".

- Which files does c:/SFML/lib/cmake/SFML contain?
- Which SFML version is located in c:/SFML?
- Did you build SFML yourself or download a precompiled version from their website?
- Which MinGW version are you using?

luka

Quote from: texus on 26 March 2024, 08:40:15The set(SFML_DIR "c:/SFML/lib/cmake/SFML") should be sufficient when placed before the find_package or add_subdirectory line.

TGUI tries to find SFML with "find_package(SFML 2 CONFIG COMPONENTS graphics REQUIRED)".

- Which files does c:/SFML/lib/cmake/SFML contain?
- Which SFML version is located in c:/SFML?
- Did you build SFML yourself or download a precompiled version from their website?
- Which MinGW version are you using?

Thank you for the prompt response and assistance.

1. alle the cmake files
   - SFMLConfig.cmake
   - SFMLConfigDependencies.cmake
   - SFMLConfigVersion.cmake
   - SFMLSharedTargets.cmake
   - SFMLSharedTargets-debug.cmake
   - SFMLSharedTargets-release.cmake
   - SFMLStaticTargets.cmake
   - SFMLStaticTargets-debug.cmake
   - SFMLStaticTargets-release.cmake
2. SFML 2.6.0 is installed
3. I downloaded a precompiled version from their website.
4. I am using GCC 13.1.0 MinGW 64-bit

texus

#3
I'm a bit confused about how the project is being build.

The log contained the following line:
Quote[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=C:\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=C:\mingw64\bin\g++.exe -SC:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/TGUI -Bc:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build -G "MinGW Makefiles"

The source directory is being set to the "TGUI" subdirectory, but the build directory is the one from your project? That would mean that it is trying to build TGUI and your own cmake script isn't even being executed.

If that command where to contain "-DSFML_DIR=c:/SFML/lib/cmake/SFML" then it would probably build TGUI successfully. However that would only build TGUI and not your project, so you probably want to change the source directory instead.

luka

#4
I apologize for any confusion. I am new to CMake, so my knowledge is somewhat limited. I have only used CMake for incorporating SFML into two projects, and you can see how I did that in my CMakeLists.txt files, which compile without any issues. I referred to this blog post (https://www.ics.com/blog/find-and-link-libraries-cmake) to integrate the TGUI library, so I'm having a bit of trouble following your steps.

QuoteThe source directory is being set to the "TGUI" subdirectory, but the build directory is the one from your project? That would mean that it is trying to build TGUI and your own cmake script isn't even being executed.

I tried something different. Here is the orignale Cmakelists.txt:
cmake_minimum_required(VERSION 3.17)
project(AutoBackupScript VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)

include_directories(headers)
add_subdirectory(TGUI)

# Füge die Quelldateien hinzu
set(SOURCES
    src/main.cpp

)


# Füge das ausführbare Ziel hinzu und verlinke ImGui
add_executable(AutoBackupScript ${SOURCES})

set(TGUI_DIR "C:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/TGUI/cmake")
find_package(TGUI 1 REQUIRED)

target_link_libraries(AutoBackupScript PRIVATE TGUI)

# Suche und verlinke SFML
set(SFML_DIR "c:/SFML/lib/cmake/SFML")  # Pfad zum SFML CMake-Modul
find_package(SFML COMPONENTS system window graphics network audio REQUIRED)
if (SFML_FOUND)
    target_link_libraries(AutoBackupScript PRIVATE sfml-system sfml-window sfml-graphics sfml-network sfml-audio)
endif()

# Kopiere DLLs in das Build-Verzeichnis
if(WIN32)
    file(GLOB BINARY_DEP_DLLS "c:/SFML/bin/*.dll")
    file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
    file(GLOB MINGW_DEP_DLLS "C:/mingw64/bin/*.dll")
    file(COPY ${MINGW_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
endif()


but i encountered a similar problem.
[main] Building folder: AutoBackupScript2
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build --config Debug --target all -j 6 --
[build]
[build] Searching for SFML 2...
[build]
[build] -- Could NOT find SFML (missing: SFML_DIR)
[build]
[build] Searching for SFML 3...
[build]
[build] -- Could NOT find SFML (missing: SFML_DIR)
[build] CMake Error at TGUI/cmake/Dependencies.cmake:80 (message):
[build]   CMake couldn't find SFML.
[build]
[build]   Set SFML_DIR to the directory containing SFMLConfig.cmake (usually
[build]   something like SFML_ROOT/lib/cmake/SFML)
[build]
[build] Call Stack (most recent call first):
[build]   TGUI/cmake/Dependencies.cmake:93 (tgui_find_dependency_sfml)
[build]   TGUI/src/Backend/CMakeLists.txt:153 (tgui_add_dependency_sfml)
[build]   TGUI/src/CMakeLists.txt:305 (include)
[build]
[build]
[build] -- Configuring incomplete, errors occurred!
[build] mingw32-make: *** [Makefile:178: cmake_check_build_system] Error 1
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build --config Debug --target all -j 6 -- exited with code: 2
[driver] Build completed: 00:00:00.482
[build] Build finished with exit code 2


Thank you for your time and assistance.

texus

Now the log output looks more normal.
You need to move the set(SFML_DIR "c:/SFML/lib/cmake/SFML") line in your script to ABOVE the "add_subdirectory(TGUI)" line.

Also, your code should either contain "add_subdirectory(TGUI)" or "set(TGUI_DIR ...)\n find_package(TGUI 1 REQUIRED)", but not both of these.

luka

I deeply appreciate your assistance thus far. Nevertheless, I've encountered a snag with the GUI builder.

[main] Building folder: AutoBackupScript2
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build --config Debug --target all -j 6 --
[build] [  3%] Linking CXX executable AutoBackupScript.exe
[build] [ 81%] Built target tgui
[build] C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lTGUI: No such file or directory
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\AutoBackupScript.dir\build.make:104: AutoBackupScript.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:132: CMakeFiles/AutoBackupScript.dir/all] Error 2
[build] mingw32-make[1]: *** Waiting for unfinished jobs....
[build] [ 96%] Built target gui-builder
[build] mingw32-make: *** [Makefile:135: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build --config Debug --target all -j 6 -- exited with code: 2
[driver] Build completed: 00:00:00.913
[build] Build finished with exit code 2


texus

The error isn't with the gui builder. If you set TGUI_BUILD_GUI_BUILDER to OFF then you would get the same error.

The library name passed to "target_link_libraries" should be either "TGUI::TGUI" (recommended) or "tgui" (deprecated), but "TGUI" is wrong.

The cmake command that is being executed has "-j 6" in it. Is this something you can change yourself? The log output would have been more clear if you ran it with "-j 1" (which you should only do when investigating an issue, as usually you do want to build with more threads). Now it builds your project and the gui builder at the same time so it prints both outputs interleaved and it isn't clear that the error is about your project and not the gui builder.

luka

Quote from: texus on 27 March 2024, 19:28:51The library name passed to "target_link_libraries" should be either "TGUI::TGUI" (recommended)

I've managed to compile this file now.
#include <TGUI/TGUI.hpp>
#include <TGUI/Backend/SFML-Graphics.hpp>


int main()
{

}

I now have an issue with the example for the SFML backend, encountering an undefined reference to the bool runExample(tgui::BackendGui& gui) function.

[main] Building folder: AutoBackupScript2
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build --config Debug --target all -j 6 --
[build] [ 77%] Built target tgui
[build] [ 81%] Building CXX object CMakeFiles/AutoBackupScript.dir/src/main.cpp.obj
[build] [ 96%] Built target gui-builder
[build] [100%] Linking CXX executable AutoBackupScript.exe
[build] C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\AutoBackupScript.dir/objects.a(main.cpp.obj): in function `main':
[build] C:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/src/main.cpp:41: undefined reference to `runExample(tgui::BackendGui&)'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make[2]: *** [CMakeFiles\AutoBackupScript.dir\build.make:108: AutoBackupScript.exe] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:132: CMakeFiles/AutoBackupScript.dir/all] Error 2
[build] mingw32-make: *** [Makefile:135: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/luka-/OneDrive/Desktop/AutoBackupScript2/build --config Debug --target all -j 6 -- exited with code: 2
[driver] Build completed: 00:00:05.274
[build] Build finished with exit code 2

Quote from: texus on 27 March 2024, 19:28:51The cmake command that is being executed has "-j 6" in it. Is this something you can change yourself? The log output would have been more clear if you ran it with "-j 1" (which you should only do when investigating an issue, as usually you do want to build with more threads). Now it builds your project and the gui builder at the same time so it prints both outputs interleaved and it isn't clear that the error is about your project and not the gui builder.

There is a way, is that something I should consider in general or is it more something for debugging?

texus

QuoteI now have an issue with the example for the SFML backend, encountering an undefined reference to the bool runExample(tgui::BackendGui& gui) function.

The TGUI examples found in the "examples" folder are split into 2 parts. The first part contains just the main() function and shows how to initialize the window and gui (using backend-specific code, in this case using SFML Graphics). The second part is the "runExample" function that contains the code that is backend-independent and which is thus the same no matter whether you are using SFML, SDL or GLFW. The second part is found inside either the "scalable_login_screen" or "many_different_widgets" subfolder.

I'm guessing you only have a declaration like "bool runExample(tgui::BackendGui& gui);" in your current code. You can replace it with the following to have an example that compiles:
Code (cpp) Select
bool runExample(tgui::BackendGui& gui)
{
    return true;
}

The website contains some more example codes: https://tgui.eu/examples/latest-stable/

texus

Quoteis that something I should consider in general or is it more something for debugging?

It's only for debugging.
The higher the number, the faster the project will build (as the number indicates how many source files can be compiled simultaneously). At least as long as the number doesn't exceed the amount of CPU threads your processor has (a higher number will work but won't make it faster) and as long as you have sufficient RAM (which becomes important for very high values).
Setting it to 1 means that all operations are done sequentially and nothing is done in parallel. This makes building much slower, but then the logs will properly show you which step is being executed when the error happens.

luka

#11
Quote from: texus on 27 March 2024, 22:45:23
Quoteis that something I should consider in general or is it more something for debugging?

It's only for debugging.
The higher the number, the faster the project will build (as the number indicates how many source files can be compiled simultaneously). At least as long as the number doesn't exceed the amount of CPU threads your processor has (a higher number will work but won't make it faster) and as long as you have sufficient RAM (which becomes important for very high values).
Setting it to 1 means that all operations are done sequentially and nothing is done in parallel. This makes building much slower, but then the logs will properly show you which step is being executed when the error happens.

Good to know. I believe I'll need to delve deeper into CMake to fully utilize it. Thank you very much for your patience and guidance. Everything is functioning now.

luka

Apologies for bothering you once more. Despite the initial joy of successfully compiling the program, it appears to crash every time I attempt to run it, without any error messages. Could I be overlooking something, such as copying .dll files or a similar requirement?

texus

QuoteCould I be overlooking something, such as copying .dll files or a similar requirement?
It's possible, but it's hard to say if you don't actually get an error message. Are the sfml and dll files placed next to the exe?

I don't really know VS Code. Do you have some process output somewhere? If the program crashes then it usually sets a return value other than 0. If you find some output with something similar to "program exited with code ..." then you can usually search online for that code to determine whether it ended due to a missing dll, an access violation or an uncaught exception.

There are 2 things you can test:
1) Run the exe from file explorer instead of from the IDE. If it won't run due to a missing dll, you will get an error when attempting to run the exe like that.
2) If something goes wrong in TGUI (e.g. a file can't be loaded), then it will throw an exception. If you don't catch this exception then the program terminates.

Do something like this in the main function to print something to the command line when an exception occurs, so that you can see what went wrong. I'm not sure if you will be able to see the text printed somewhere, but you might also be able to e.g. put a breakpoint in the catch code to test if it passes there. The "e.what()" value is a string that contains the TGUI error message.
Code (cpp) Select
int main()
{
    try
    {
        // <Put your existing code here>
        return 0;
    }
    catch (const tgui::Exception& e)
    {
        std::cerr << "TGUI exception: " << e.what() << std::endl;
        return 1;
    }
}

luka

Quote from: texus on 28 March 2024, 08:39:33It's possible, but it's hard to say if you don't actually get an error message. Are the sfml and dll files placed next to the exe?

The SFML .dll files are present, but when attempting to run the .exe directly from the build directory, I receive an error stating that the tgui-d.dll is not found. Where can I find it?