0

I'm new to C++ and CMake and started a big project. Im using VScode as my text editor and CMake as my build tool.

The problem is that i can include "SFML/Graphics.hpp in all my header files that has a source file with the same name, but in header-only files if i include "SFML/Graphics.hpp" or any other file that include "SFML/Graphics" cmake won't build and returns an error.

Here's is my code

Project Directory Tree

Game|> (root)
  |- CMakeLists.txt
  |> include
  |    |- BaseComponent.hpp (Header-only)
  |    |- TransformComponent.hpp (Header-only)
  |    |- CombatComponent.hpp (Header-only)
  |    |- MovementComponent.hpp (Header-only)
  |    |- Constants.hpp (Header-only)
  |    |- TimeUtils.hpp (Header-only)
  |    |- EcsManager.hpp
  |    |- EntityManager.hpp
  |    |- Game.hpp
  |    |- GameContext.hpp (Header-only)
  |    |- Scene.hpp (Header-only)
  |    |- GameScene.hpp
  |    |- System.hpp
  |    |- Input.hpp
  |    |- MovementSystem.hpp
  |    |- RenderableSystem.hpp
  |> src
  |    |- CMakeLists.txt
  |    |- main.cpp
  |    |- Game.cpp
  |    |- EntityManager.cpp
  |    |- SceneManager.cpp
  |    |- Input.cpp
  |    |- GameScene.cpp
  |    |> Ecs
       |    |- CMakeLists.txt
       |    |- EcsManager.cpp
       |    |- System.cpp
       |    |> Systems
            |    |- CMakeLists.txt
            |    |- MovementSystem.cpp
            |    |- RenderableSystem.cpp
  |    
  |> bin
  |> build

CMakeLists.txt - Root

project(RPG)
set(CMAKE_TOOLCHAIN_FILE         
"C:/Users/dante/vcpkg/scripts/buildsystems/vcpkg.cmake")

set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin")
set(SOURCES src/EntityManager.cpp src/Game.cpp src/GameScene.cpp 
src/main.cpp src/SceneManager.cpp src/Input.cpp)
include_directories("${PROJECT_SOURCE_DIR}/include")

add_subdirectory(src)

find_package(SFML 2.5.1 COMPONENTS REQUIRED graphics window system network 
audio)
find_package(nlohmann_json 3.2.0 REQUIRED)

add_executable(RPG ${SOURCES}) 
target_link_libraries(RPG sfml-graphics sfml-window sfml-system sfml- 
network sfml-audio)
target_link_libraries(RPG nlohmann_json::nlohmann_json)
target_link_libraries(RPG ECS)

CMakeLists.txt - src

add_subdirectory(Ecs)

CMakeLists.txt - Ecs

add_subdirectory(Systems)

add_library(ECS EcsManager.cpp System.cpp )

target_link_libraries(ECS ECS_SYSTEMS)

CMakeLists.txt - Systems

add_library(ECS_SYSTEMS MovementSystem.cpp RenderableSystem.cpp)

The erro that Cmake outputs is : 'SFML/Graphics.hpp': No such file or directory MSVC(C1083)

If i include "SFML/Graphics.hpp" in any "Any"Component Class it gives error If i include "SFML/Graphics.hpp" in a .hpp file that have a .cpp file associated with it and include that class in "Any"Component Class (or any other header-only) it gives error.

I can't find any solution to that in stackoverflow.

Don't judge my folder structure it was different when the error first came up... than i changed to that to see if the CMake would build.

drescherjm
  • 10,365
  • 5
  • 44
  • 64

1 Answers1

0

You need to add the SFML include directory to the list of include directories.

After

find_package(SFML 2.5.1 COMPONENTS REQUIRED graphics window system network audio)

add the line

include_directories(${SFML_INCLUDE_DIR})
Jason
  • 3,777
  • 14
  • 27
  • Thta didn't solve the problem... still the same error. Im using vcpkg to manage packages @Jason – Dante Araújo Nov 12 '19 at 23:45
  • @DanteAraújo What happens if you add the line `message(${INCLUDE_DIRECTORIES})`? – Jason Nov 13 '19 at 04:17
  • it doesn't show anything. Any other message it shows normally but if i message the include_directories variable doesn't show anything. – Dante Araújo Nov 13 '19 at 15:31
  • if i added get_directory_property(prop INCLUDE_DIRECTORIES) message(${prop}) than it shows my project_source_dir/include – Dante Araújo Nov 13 '19 at 15:36
  • @DanteAraújo What happens if you add `message(${SFML_INCLUDE_DIR})`? – Jason Nov 13 '19 at 18:55
  • sorry for late reply, but it shoes nothing :(. My solution was to remove all folder in the include directory and put every hpp file inside the include directory with no subfolders, then i could find sfml in hearder-only files.. – Dante Araújo Nov 15 '19 at 12:39
  • @DanteAraújo Try using `include_directories(${SFML_INCLUDE_DIRS})` (my fault). If that doesn't work you may want to check the value of `SFML_FOUND`. – Jason Nov 21 '19 at 03:16
  • I already changed all the folder structure and put all files inside src folder now i don't have any problem – Dante Araújo Dec 09 '19 at 17:50