Search code examples
c++visual-studio-codesfml

VSCode : Images can't be opened when debugging(C++/SFML)


I am creating a little online game with SFML (C++). My server is in C++ with SFML and my client is in C++ with SFML.

My problem is quite simple, but I just can't find the answer on Google :

I am using images for my client. I use the SFML functions to load images and it works fine when running the client normally, but when I use the GDB debugger with VScode, it does not load my images and I get this error for each images :

Failed to load image "../myImage.png". Reason: Unable to open file

Here is my debug configuration file :

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/FarmersBuilds/farmers.launcher",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

And here is my build task file (everything works when running the executable generated with this without debugger) :

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "clientbuild",
            "type": "shell",
            "command": "cd FarmersBuilds/BuildObjects; g++ -g -c ../../main.cpp ../../GameScreen.cpp ../../LoginScreen.cpp ../../RegisterScreen.cpp ../../ResourceManager.cpp ../../RoomConnector.cpp ../../ScreenParameters.cpp; g++ -g main.o GameScreen.o LoginScreen.o RegisterScreen.o ResourceManager.o RoomConnector.o ScreenParameters.o -o ../farmers.launcher -lpthread -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network; ../farmers.launcher",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

Here is an example of a constant I use to define the path of my files :

const std::string PATH_GRASS_FILE = "../grass.png";

Here is an example of how I load my images :

if (!loginScreenBackgroundTexture->loadFromFile(PATH_GRASS_FILE)) { ... }

Is there something I need to do to allow debugger to use my images or a paramater I must add in the debugger configuration file?

P.S. I am develeopping on Linux.

Thanks Cratebox99


Solution

  •         "command": "cd FarmersBuilds/BuildObjects; g++ -g -c ../../main.cpp ../../GameScreen.cpp ../../LoginScreen.cpp ../../RegisterScreen.cpp ../../ResourceManager.cpp ../../RoomConnector.cpp ../../ScreenParameters.cpp; g++ -g main.o GameScreen.o LoginScreen.o RegisterScreen.o ResourceManager.o RoomConnector.o ScreenParameters.o -o ../farmers.launcher -lpthread -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network; ../farmers.launcher",
    

    The above line runs the executable from within the FarmersBuilds/BuildObjects directory, which is then the Current Working Directory of the process. You probably want to cd ..; ./farmers.launcher instead.