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

Author Topic: having trouble adding a SFML to my project using CMake  (Read 1518 times)

0 Members and 1 Guest are viewing this topic.

jamesfuentes

  • Newbie
  • *
  • Posts: 1
    • View Profile
having trouble adding a SFML to my project using CMake
« on: March 06, 2024, 11:12:51 am »
My IDE is CLion, using CMake and SFML.
I've been trying to figure this but for whatever reason, my brain is acting like the smoothest sphere you've ever seen.
my project structure is:
MyProject
    sfml
        bin
        include
            SFML
                Audio
                    // various .hpp files
                Graphics
                    // various .hpp files
                Network
                    // various .hpp files
                System
                    // various .hpp files
                Window
                // various .hpp files
        lib
    CMakeLists.txt
    main.cpp
and i have this in my CMakeLists.txt
cmake_minimum_required(VERSION 3.26)
project(byte)
set(CMAKE_CXX_STANDARD 17)
add_executable(MyProject main.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/sfml/include)
As far as i understand, the include_directories is basically bringing in the header files, but im having trouble with target_link_libraries. i've tried using
target_link_libraries(MyProject ${CMAKE_CURRENT_SOURCE_DIR}/sfml/lib)
but CMake drops that item, saying it may only link to libraries. What is it that i am missing in this?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: having trouble adding a SFML to my project using CMake
« Reply #1 on: March 06, 2024, 11:22:59 am »
I highly recommend to use the SFML CMake Template: https://www.sfml-dev.org/tutorials/2.6/start-cmake.php

Not only will you not have to build SFML separately anymore, but it also provides a valid and ready to use CMake file.

One of the strength of CMake is that you can provide configuration files for your library, so that any users don't have to manually specify include directories or library file names, instead they can work with targets and have the config do all the heavy lifting, which additionally makes it platform unspecific CMake code.

If you don't want to use the CMake template, you should really be calling find_package and set the SFML_DIR to point to the lib/cmake/SFML directory or SFML_ROOT to point to the SFML root directory.
Then you don't have specify an include directory and you can just have target_link_libraries(MyProject sfml-graphics sfml-audio)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/