c++compiler-errorsdirectorydependencies

C++ Dependencies directories between files


My compiler has a problem to search files in the directories. I'm on windows 10.

First of all, my files are organized in this way:

I have 9 files .hpp which are:

And I have my main.cpp which is an example to see if my SMFL works:

#include <Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(shape);
        window.display();
    }
    return 0;
}

The problem is my compiler says: "No such file or directory" with #include <SFML/Window.hpp> in red.

I tried to find a solution through VisualStudioCode with the IncludePaths inside my .json on each file .hpp but it doesn't work.

I also tried on my terminal to put a command: g++ -I/SFML/include main.cpp -o EX but I have the error above.

Or: g++ -I/SFML/include/** main.cpp -o EX but this time it's #include <SFML/System.hpp> which is in red.

I don't understand why he put this error because I don't include it... It seems to not recognize my paths or to have a dependency between the .hpp. Maybe the solution is inside the directory "lib" but I don't know how to specify it or to manipulate it.

I'm blocked since yesterday and I hope you will help me.


Solution

  • if i was you, i wouldn't bother making a makefile myself.

    Instead i will use cmake to generate the makefile and let him compile both my code and SFML.

    If you want to do it like that, you can just download cmake on their website.

    then in your project create a file named

    in your CMakeLists copy and paste that code for sfml with c++11

    cmake_minimum_required(VERSION 3.19)
    project(ProjectNAME)
    
    # Source files
    set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/source")
    set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")
    
    #All the sources files listed
    #List only the cpp file, header are automatically listed
    set(SOURCES
            "${SRC_DIR}/main.cpp"
            )
    
    
    # Executable definition and properties
    add_executable(${PROJECT_NAME} ${SOURCES})
    target_include_directories(${PROJECT_NAME} PRIVATE "${SRC_DIR}")
    set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
    
    # SFML
    set(SFML_DIR "${LIB_DIR}/SFML")
    
    # this line is here to avoid having to move the dll into the build folder
    set(BUILD_SHARED_LIBS FALSE)
    
    
    add_subdirectory("${SFML_DIR}")
    target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-audio sfml-system sfml-window sfml-network)
    

    Don't forget to put your main.cpp in the source folder and your SFML folder in the Lib Folder. Pay attention to name correctly all the folder in the CMakeLists.txt

    then if you are on linux with gcc or g++:

    if you are on window with mingw (don't forget to add mingw in your path):

    And everything should work easily, without having to configure the make file yourself. And if you want to learn more about cmake they have a great documentation

    Now you just have to include <SFML/graphics.hpp>