-4

I am using vs code for c++ coding and i use sfml library for graphic programing . I draw a shape first and it very good but when I want use sfml library with the same code , vs code show me many errors like this :

' main.o:main.cpp:(.text+0xf0): undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'

Here is my code and it is a simple circle in a window and it worked before very fine .

#include <SFML/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;
}

this is my command to tell the compiler to find sfml headers in vs cod terminal :

g++ -c main.cpp -I/include

and this my command to tell the linker for finding sfmal libraries(.so files)

g++ main.o -o sfml-app -L/lib -lsfml-graphics -lsfml-window -lsfml-system this is my c_cpp_properties.json file

{
"configurations": [
    {
        "name": "Win32",
        "includePath": [
            "${workspaceFolder}/**",
            "C:\\SFML-2.5.1\\include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "intelliSenseMode": "windows-gcc-x64",
        "compilerPath": "C:\\MinGW\\bin\\g++.exe"
    }
],

"version": 4

} and this is my task.json file

{
"tasks": [
    {
        "type": "cppbuild",
        "label": "C/C++: g++.exe build active file",
        "command": "C:\\MinGW\\bin\\g++.exe",
        "args": [
            "-fdiagnostics-color=always",
            "-g",
            "${file}",
            "-o",
            "${fileDirname}\\${fileBasenameNoExtension}.exe"
        ],
        "options": {
            "cwd": "${fileDirname}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "detail": "Task generated by Debugger."
    }
],
"version": "2.0.0"

} and this the errors messages :

errors pic

sasan
  • 1
  • 1
  • 1
    Please [edit] your question to include the *full* and *complete* build log. Also please show us the `tasks.json` file you use to build. – Some programmer dude Aug 28 '23 at 07:17
  • 1
    "undefined reference ..." is a linker error. The problem is not in your code. It compiles fine, but fails to link. The issue is in how you link the library. https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix – 463035818_is_not_an_ai Aug 28 '23 at 07:20
  • 3
    The `c_cpp_properties.json` file is for the IntelliSense, not for building. The `tasks.json` file is for building. Please take some time to [read the documentation](https://code.visualstudio.com/docs/cpp/config-mingw). – Some programmer dude Aug 28 '23 at 07:34
  • 2
    Your `tasks.json` doesn't link with the SFML libraries. Please learn about [the difference between a header file and a library](https://stackoverflow.com/questions/61600645/what-is-the-difference-between-header-and-library-file). – Some programmer dude Aug 28 '23 at 07:49
  • If you build on the command-line you use the command `g++ main.o -o sfml-app -L/lib -lsfml-graphics -lsfml-window -lsfml-system`. This is not the command you use in the `tasks.json` file. Where is the `-L` option? Where are the libraries with their `-l` options? – Some programmer dude Aug 28 '23 at 08:22
  • so what changes do i must in my task.json file – sasan Aug 28 '23 at 08:36
  • 1
    I recommend a different IDE. VSCode isn't recommended for beginners. Use something like Visual Studio, CLion or Eclipse CDT. – jabaa Aug 28 '23 at 11:29
  • ***so what changes do i must in my task.json file*** Look at the command line in these examples: [https://en.sfml-dev.org/forums/index.php?topic=11535.0](https://en.sfml-dev.org/forums/index.php?topic=11535.0) [https://stackoverflow.com/questions/23397536/how-to-compile-sfml-with-mingw](https://stackoverflow.com/questions/23397536/how-to-compile-sfml-with-mingw) you probably need to add something similar to: `-lsfml-graphics -lsfml-window -lsfml-system -lopengl32 -lwinmm -lgdi32` to your arguments. – drescherjm Aug 28 '23 at 17:41

0 Answers0