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

Author Topic: CMake include problem  (Read 5187 times)

0 Members and 1 Guest are viewing this topic.

Masalan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
CMake include problem
« on: June 10, 2021, 09:06:35 pm »
Hello. Working on Windows. Im trying to dive in gamedev, but this thing is blocking me.
Im trying to include "SFML/Graphics.hpp" in one of my header files, located in src folder. Main.cpp is located in the same directory.
#pragma once

#include <SFML/Graphics.hpp>

class Window
{
public:
    Window(const std::string& window_name);

    void update();

    void beginDraw();
    void Draw(const sf::Drawable& drawable);
    void endDraw();

    bool isOpen() const;

private:
    sf::RenderWindow window;
}
 
This is the trouble header. When compiling it says that it cant find SFML/Graphics.

CMakeLists.txt, located out of src folder.
cmake_minimum_required(VERSION 3.20.3)

project(SNAKE)

add_subdirectory(src)


set(SFML_DIR "c:\\SFML\\lib\\cmake\\SFML\\")
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-audio)
 

window.hpp is located in src folder. CMakeLists.txt in src folder:
add_executable(${PROJECT_NAME} main.cpp)


add_library(window window.cpp window.hpp)


set(LIBRARIES window)
target_link_libraries(${PROJECT_NAME} ${LIBRARIES})
 

Error log:
In file included from E:\GameDev\SFML\Snake\src\window.cpp:1:
E:\GameDev\SFML\Snake\src\window.hpp:3:10: fatal error: SFML/Graphics.hpp: No such file or directory
 #include <SFML/Graphics.hpp>
 

Whats interesting, main.cpp located in the same directory has no trouble including "SFML/Graphics".
Im new to cmake and dont know how things work. Can someone help me to make "window.hpp" include "SFML/Graphics.hpp" correctly?
« Last Edit: June 10, 2021, 09:13:00 pm by Masalan »

nogoodname

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: CMake include problem
« Reply #1 on: June 13, 2021, 03:59:44 am »
You would need to add this to your CMake script.

target_link_libraries("window" PUBLIC
    "sfml-graphics"
    "sfml-audio"
)
 
« Last Edit: June 13, 2021, 04:02:40 am by nogoodname »

Masalan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: CMake include problem
« Reply #2 on: June 13, 2021, 06:50:00 am »
You would need to add this to your CMake script.

target_link_libraries("window" PUBLIC
    "sfml-graphics"
    "sfml-audio"
)
 

Thanks for the reply.

I have solved it by adding add_compile_options(-I c:/SFML/include/) before add_subdirectory(src)

Your solution is good, but what if I have much more libs? Is there any flexible solution than adding sfml libs to every lib I wrote on my own?
« Last Edit: June 13, 2021, 08:19:06 am by Masalan »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: CMake include problem
« Reply #3 on: June 14, 2021, 09:22:44 am »
The better solution is to put find_package before the add_subdirectory, so once CMake descends into the sub-directory, SFML is already found and available.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Masalan

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: CMake include problem
« Reply #4 on: June 15, 2021, 08:30:13 pm »
Thank you, I'll definetely try it. Thanks a lot for help.

 

anything