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

Author Topic: [SOLVED]SFML integration via CMakeList.txt (static library)  (Read 4366 times)

0 Members and 1 Guest are viewing this topic.

ChupiChulio

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED]SFML integration via CMakeList.txt (static library)
« on: August 10, 2023, 11:07:16 am »
Hello together,

I am relatively new to this topic and trying to get SFML running in my environment.

Currently I have tried the console commands, as in tutorials on SFML website and it worked fine.
For static library generation as well, using with -s attached ;
g++ main.o -o sfml-app -L<sfml-install-path>/lib -lsfml-graphics-s -lsfml-window-s -lsfml-system-s

Now I have tried to trigger the whole proccess via CMakeList.txt in VSCode, I made it so far executing the example using dynamic library access (dll must be present within the same folder as .exe file)
Now two points here:
  • I am not sure how to implement static library in my CMakeList.txt file, to get rid of extra dll files ?
  • When executing .exe I am getting terminal window opened in background, how to get rid of it ?

Thank you

cmake_minimum_required(VERSION 3.0.0)
project(sfml_cmake_headers VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

set(CMAKE_CXX_STANDARD 14)

#If you are using Windows, manually set the location where you installed SFML.
set(SFML_DIR "C:/Users/User/Documents/Libraries/SFML-2.6.0/lib/cmake/SFML")
#set(SFML_INCLUDE_DIR "C:/Users/User/Documents/Libraries/SFML-2.6.0/include/SFML")
   

add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../../Users/User/Documents/Libraries/SFML-2.6.0/include)


find_package(SFML COMPONENTS network audio graphics window system REQUIRED)
if(SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  # SFML version >= 2.5
  #In SFML Version 2.5 or later, ${SFML_LIBRARIES}, ${SFML_DEPENDENCIES}, ${SFML_INCLUDE_DIR} no longer exist. Specify libraries individually.
  target_link_libraries(sfml_cmake_headers sfml-network sfml-audio sfml-graphics sfml-window sfml-system)
endif()
 
« Last Edit: August 16, 2023, 09:26:26 pm by ChupiChulio »

Thrasher

  • SFML Team
  • Jr. Member
  • *****
  • Posts: 57
    • View Profile
Re: SFML integration via CMakeList.txt (static library)
« Reply #1 on: August 10, 2023, 05:09:15 pm »
Luckily the perfect solution exists for you!

https://github.com/SFML/cmake-sfml-project

Check out the official SFML CMake project template. The README even has some information on how to get static libraries.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML integration via CMakeList.txt (static library)
« Reply #2 on: August 14, 2023, 02:01:57 pm »
If you use find_package to find SFML libraries, you need to call set(SFML_STATIC_LIBRARIES TRUE) beforehand to look for static SFML libraries.

See also the CMakeConfig documentation: https://github.com/SFML/SFML/blob/11b73743c42cf7ecd7c596ba83fdbf1150ffa96c/cmake/SFMLConfig.cmake.in#L24-L25
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ChupiChulio

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML integration via CMakeList.txt (static library)
« Reply #3 on: August 14, 2023, 02:03:50 pm »
@Thrasher: I've checked the link, but this doesnt really help sme a lot. Or maybe I can not filter the neccessary information out of it.

I have tried few things out and got stuck with the inclusion of the freetype depencency.
I had to install it initially (rather generate the .dll and .lib files using VisualStudio)
but cmake cant find the libraries, whats quite strage.

find_package() doesnt throw an error, so I assupe that freetype is actually found...

cmake_minimum_required(VERSION 3.0.0)
#set(CMAKE_CXX_FLAGS "-gdwarf-4")

project(sfml_static_include VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_PREFIX_PATH "C:/Users/myId/Documents/Libraries/freetype-2.13.0")

#If you are using Windows, manually set the location where you installed SFML.
set(SFML_DIR "C:/Users/myId/Documents/Libraries/SFML-2.6.0/lib/cmake/SFML")
set(SFML_INCLUDE_DIR "C:/Users/myId/Documents/Libraries/SFML-2.6.0/include")
set(FREETYPE_LIBRARY_DEBUG "C:/Users/myId/Documents/Libraries/freetype-2.13.0/objs/x64/Debug/freetype.lib")
   
set(SFML_STATIC_LIBRARIES TRUE)

find_package(SFML COMPONENTS graphics window system REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE SFML_INCLUDE_DIR)
target_include_directories(${PROJECT_NAME} PRIVATE ${FREETYPE_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} ${FREETYPE_LIBRARY_DEBUG})

find_package(Freetype 2.13.0 REQUIRED)

if(SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  # SFML version >= 2.5
  #In SFML Version 2.5 or later, ${SFML_LIBRARIES}, ${SFML_DEPENDENCIES}, ${SFML_INCLUDE_DIR} no longer exist. Specify libraries individually.
  target_link_libraries(${PROJECT_NAME}
        #${SFML_INCLUDE_DIR}/sfml-network-s
        #${SFML_INCLUDE_DIR}/sfml-audio-s
        sfml-graphics
        sfml-window
        sfml-system
        opengl32
        winmm
        gdi32
        freetype
        )
endif()
 

error message:
[cmake] -- Found SFML 2.6.0 in C:/Users/myId/Documents/Libraries/SFML-2.6.0/lib/cmake/SFML
[cmake] -- Found Freetype: C:/Users/myId/Documents/Libraries/freetype-2.13.0/objs/x64/Debug/freetype.lib (found suitable version "2.13.0", minimum required is "2.13.0")
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: C:/myFolder/CPP/SFML/sfml_static_include/build
[build] Starting build
[proc] Executing command: C:\toolbase\cmake\3.20.2\bin\cmake.EXE --build c:/myFolder/CPP/SFML/sfml_static_include/build --config Debug --target all -j 10 --
[build] [ 50%] Building CXX object CMakeFiles/sfml_static_include.dir/main.cpp.obj
[build] [100%] Linking CXX executable sfml_static_include.exe
[build] c:/toolbase/mingw/comp_10.4.0_multilib_1f/bin/../lib/gcc/x86_64-w64-mingw32/10.4.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lfreetype
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\sfml_static_include.dir\build.make:104: sfml_static_include.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/sfml_static_include.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:90: all] Error 2
[proc] The command: C:\toolbase\cmake\3.20.2\bin\cmake.EXE --build c:/myFolder/CPP/SFML/sfml_static_include/build --config Debug --target all -j 10 -- exited with code: 2
[driver] Build completed: 00:00:01.799
[build] Build finished with exit code 2
 

UPDATE
@exploit3r thanks, I had included the macro already, but the issue is coming up as described above...
« Last Edit: August 14, 2023, 02:12:22 pm by ChupiChulio »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML integration via CMakeList.txt (static library)
« Reply #4 on: August 14, 2023, 02:12:18 pm »
You don't and shouldn't manually search for freetype, SFML should automatically do that and add the dependencies to the SFML targets.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ChupiChulio

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML integration via CMakeList.txt (static library)
« Reply #5 on: August 14, 2023, 02:44:03 pm »
I dont understand, why freetype cant be found.
I could remove the find_package(Freetype 2.13.0 REQUIRED) but stii the issue is present.

I have even linked the freetype.lib location ( the folder where library file is to be found) to my environmental variables, still cmake cannot find it...

What I do see as well, is the cmakecashe.txt file is trying to access some .a file which is nonexistant;

//Path to a library.
FreeType_LIB:FILEPATH=C:/Users/myId/Documents/Libraries/SFML-2.6.0/lib/libfreetype.a

//Path to a file.
SFML_DOC_DIR:PATH=SFML_DOC_DIR-NOTFOUND

//Value Computed by CMake
sfml_static_include_BINARY_DIR:STATIC=C:/myFolder/CPP/SFML/sfml_static_include/build

//Value Computed by CMake
sfml_static_include_SOURCE_DIR:STATIC=C:/myFolder/CPP/SFML/sfml_static_include

 

there is no libfreetype.a anywhere...
« Last Edit: August 14, 2023, 03:47:51 pm by ChupiChulio »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML integration via CMakeList.txt (static library)
« Reply #6 on: August 14, 2023, 02:59:41 pm »
Try to clear the cache and reconfigure, maybe it picked up something old and cached it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ChupiChulio

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML integration via CMakeList.txt (static library)
« Reply #7 on: August 14, 2023, 03:35:08 pm »
could there be compatibility isue, as I have generated freetype.lib and freetype.dll files in Visual Studio, but trying to access these via cmake linkeage (minGW) ?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML integration via CMakeList.txt (static library)
« Reply #8 on: August 14, 2023, 04:12:58 pm »
Is there a reason you built freetype yourself instead of using the ones provided by SFML?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ChupiChulio

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML integration via CMakeList.txt (static library)
« Reply #9 on: August 14, 2023, 04:20:23 pm »
to be honest I have asked myself the same about 30min ago, I think it was a general misunderstading "noob mode". I though if freetype cannot be found I have to install (generate) freetype files myself  ::) Now I have removed everything related to manually generated freetype files.  ???

So I am at the beginning again, with the same error, ld.exe cannot find -lfreetype

I do have the libfreetype.a inside the SFML /lib folder though...

cmake_minimum_required(VERSION 3.0.0)
#set(CMAKE_CXX_FLAGS "-gdwarf-4")

project(sfml_static_include VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

set(CMAKE_PREFIX_PATH "C:/Users/myId/Documents/Libraries/freetype-2.13.0")

#If you are using Windows, manually set the location where you installed SFML.
set(SFML_DIR "C:/Users/myId/Documents/Libraries/SFML-2.6.0/lib/cmake/SFML")
set(SFML_INCLUDE_DIR "C:/Users/myId/Documents/Libraries/SFML-2.6.0/include")
set(SFML_INCLUDE_LIB "C:/Users/myId/Documents/Libraries/SFML-2.6.0/lib")
   
set(SFML_STATIC_LIBRARIES TRUE)

find_package(SFML COMPONENTS graphics window system REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PRIVATE SFML_INCLUDE_DIR)
target_include_directories(${PROJECT_NAME} PRIVATE SFML_INCLUDE_LIB)

if(SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  # SFML version >= 2.5
  #In SFML Version 2.5 or later, ${SFML_LIBRARIES}, ${SFML_DEPENDENCIES}, ${SFML_INCLUDE_DIR} no longer exist. Specify libraries individually.
  target_link_libraries(${PROJECT_NAME}
        #${SFML_INCLUDE_DIR}/sfml-network
        #${SFML_INCLUDE_DIR}/sfml-audio
        sfml-graphics
        sfml-window
        sfml-system
        opengl32
        winmm
        gdi32
        freetype
        )
endif()

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML integration via CMakeList.txt (static library)
« Reply #10 on: August 14, 2023, 04:33:01 pm »
Remove CMAKE_PREFIX_PATH / SFML_INCLUDE_DIR / SFML_INCLUDE_LIB as well as all the include_directories calls, they are not needed.

Then make sure to start with a cleared cache.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

ChupiChulio

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: SFML integration via CMakeList.txt (static library)
« Reply #11 on: August 14, 2023, 05:02:48 pm »
I have deleted the build folder and cleared everything and rebuild, but still the same problem.

PS. the SFML MinGW 32bit version for download on the website contains a Trojan. Please remove it !!!

Thrasher

  • SFML Team
  • Jr. Member
  • *****
  • Posts: 57
    • View Profile
Re: SFML integration via CMakeList.txt (static library)
« Reply #12 on: August 14, 2023, 10:03:13 pm »
The template I shared will work for you. The code in that CMakeLists.txt will download and build SFML for you so that you can get ride of most of the CMake code you wrote yourself. Your build script will become much smaller and the linker errors will go away if you're willing to at least try it. The README in that repo explains how to use it. For the most part you just grab that build script, tweak the .cpp files to match what you have, then run 2 CMake commands.

What you're doing is over complicating things and is prone to fail in ways that are pretty confusing unless you're already very familiar with how compiling and linking C++ works. If you've spent this long struggling with your current strategy, maybe it's time to reevaluate how you're approaching this problem.

ChupiChulio

  • Newbie
  • *
  • Posts: 7
    • View Profile
[SOLVED] Re: SFML integration via CMakeList.txt (static library)
« Reply #13 on: August 16, 2023, 09:22:38 pm »
so, I have treid your approach Trasher, at least for the dynamic library access it worked, but I couldnt use the BUILD_SHARED_LIBS command to configure static library generation.

But I had modified my original approach and the static generation via CMakeList.txt worked for me.

It worked without freetype library, unfortunatelly I still couldnt figure out why freetype cannot befound as mentioned before.

cmake_minimum_required(VERSION 3.21)


project(sfml_static_include VERSION 0.1.0 LANGUAGES C CXX)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})


set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)

#set(BUILD_SHARED_LIBS FALSE)

#If you are using Windows, manually set the location where you installed SFML.
set(SFML_DIR "C:/Users/User/Documents/Libraries/SFML-2.6.0/lib/cmake/SFML")

set(SFML_STATIC_LIBRARIES TRUE)
 
find_package(SFML COMPONENTS graphics window system REQUIRED)
add_executable(${PROJECT_NAME} main.cpp)


if(SFML_FOUND)
  target_link_libraries(${PROJECT_NAME}
        sfml-graphics
        sfml-window
        sfml-system
        opengl32
        winmm
        gdi32
        #freetype
        )
endif()
 
« Last Edit: August 16, 2023, 09:24:18 pm by ChupiChulio »