SFML community forums

Help => General => Topic started by: Robonics on January 21, 2020, 03:24:53 pm

Title: Installing SFML for VS Code
Post by: Robonics on January 21, 2020, 03:24:53 pm
I am attempting to use SFML for my next project, however I have yet to find reliable information on how to install SFML for MinGW, the page on the main SFML website for SFML is for using code::blocks, and I would prefer to keep using VS Code if I could. Additionally all of the tutorials for Visual Studio are not for VS Code. I was hoping that someone who has installed it could guide me through the steps they used to install it. Thanks.

I am on Windows.
Title: Re: Installing SFML for VS Code
Post by: eXpl0it3r on January 21, 2020, 05:41:22 pm
SFML is no different to any other C++ library you could use with VS Code. You should be able to find some instructions on how to handle libraries with VS Code.
Title: Re: Installing SFML for VS Code
Post by: Robonics on January 22, 2020, 02:47:15 am
I tried several Google searches, all of which either came up with information on extensions, or were for VS and not VS Code. I've even tried adding SFML into my compiler's file to no avail.
Title: Re: Installing SFML for VS Code
Post by: sykestech on January 23, 2020, 02:25:02 pm
Hi there,

There is a few ways you can go about it, now I don't use Visual Code much beyond file editing and small websites, but you should be able to use it alongside some tools.

CMAKE

CMake is the way I would do it, but can be a little complex if you are new to all this. You can download SFML to some location and setup a CMakeList.txt to specify your project, files and dependencies, etc. You might need to download or create an FindSFML.cmake or sfml-config if one doesn't already exist (it probably does).  In fact, there is a tutorial on how to do it here: https://www.sfml-dev.org/tutorials/2.5/compile-with-cmake.php (https://www.sfml-dev.org/tutorials/2.5/compile-with-cmake.php) right from the SFML page. There are plenty of tutorials to get Cmake to work with MinGW, in fact, one of the generated projects can be  MinGW32-makefile.

Also a note, make sure that MinGW and CMake are in your PATH environmental variable, otherwise you'll run into not found command errors.

Manually

You can use the MinGW on the command line to build your code. I think the command is mingw32-g++. Bare in mind you'll have to specify all your source files and the command can become unwieldy when it grows, which is why CMake or an IDE is better for building if you don't want to manage this. Alternatively, you can add the command to a .BAT file and append source files to it as your project grows.

This might help: http://www.mingw.org/wiki/MinGW_for_First_Time_Users_HOWTO
 (http://www.mingw.org/wiki/MinGW_for_First_Time_Users_HOWTO)
and this snippet here:

https://stackoverflow.com/questions/19980043/how-do-i-use-the-mingw-compiler-from-the-command-line-on-windows
 (https://stackoverflow.com/questions/19980043/how-do-i-use-the-mingw-compiler-from-the-command-line-on-windows)

and here as well: https://courses.cs.washington.edu/courses/cse326/00wi/unix/g++.html (https://courses.cs.washington.edu/courses/cse326/00wi/unix/g++.html)

The above link shows -I and -l which can be used on the command line to link to the SFML libraries and inludes.


Use an IDE

IDEs can make your life easier and if you are new to software (or new to C++) this can just make it easier for you to get started. I can appreciate Visual Studio/CodeBlocks can feel bloated, but they do a lot of the leg work and the debugger in Visual Studio is top notch.

Hope this at least points you in the right direction.

Title: Re: Installing SFML for VS Code
Post by: Robonics on January 23, 2020, 11:19:34 pm
I wrote this as a batch file:
g++ main.cpp -IC:\SFML-2.5.1\include\SFML\Graphics.hpp -o hero.exe resources\hero.res
and tried to to compile the following code(gotten from SFML website):
#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;
}
It returns the error:
C:\Users\ezj21\OneDrive\Desktop\Utilities\Coding\LearningC++>g++ main.cpp -IC:\SFML-2.5.1\include\SFML\Graphics.hpp -o hero.exe resources\hero.res
cc1plus.exe: warning: C:\SFML-2.5.1\include\SFML\Graphics.hpp: not a directory
main.cpp:1:10: fatal error: SFML/Graphics.hpp: No such file or directory
 #include <SFML/Graphics.hpp>
          ^~~~~~~~~~~~~~~~~~~
compilation terminated.
Am I doing something wrong?
Title: Re: Installing SFML for VS Code
Post by: G. on January 23, 2020, 11:44:51 pm
Yes, the include directory is "C:\SFML-2.5.1\include\", not "C:\SFML-2.5.1\include\SFML\Graphics.hpp"
Title: Re: Installing SFML for VS Code
Post by: Robonics on January 24, 2020, 01:23:57 am
Now it gives me a long list of undefined references, but I got the code from the SFML Page:
https://www.sfml-dev.org/tutorials/2.5/start-vc.php @bottom
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x83): undefined reference to `__imp__ZN2sf6StringC1EPKcRKSt6locale'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0xa7): undefined reference to `__imp__ZN2sf9VideoModeC1Ejjj'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0xeb): undefined reference to `__imp__ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x127): undefined reference to `__imp__ZN2sf11CircleShapeC1Efy'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x130): undefined reference to `__imp__ZN2sf5Color5GreenE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x13e): undefined reference to `__imp__ZN2sf5Shape12setFillColorERKNS_5ColorE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x151): undefined reference to `__imp__ZNK2sf6Window6isOpenEv'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x170): undefined reference to `__imp__ZN2sf6Window9pollEventERNS_5EventE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x18e): undefined reference to `__imp__ZN2sf6Window5closeEv'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x1bc): undefined reference to `__imp__ZN2sf5ColorC1Ehhhh'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x1da): undefined reference to `__imp__ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x1e3): undefined reference to `__imp__ZN2sf12RenderStates7DefaultE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x1fc): undefined reference to `__imp__ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE'  
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x20f): undefined reference to `__imp__ZN2sf6Window7displayEv'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x238): undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text+0x294): undefined reference to `__imp__ZN2sf12RenderWindowD1Ev'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text$_ZN2sf11CircleShapeD1Ev[_ZN2sf11CircleShapeD1Ev]+0xf): undefined reference to `__imp__ZTVN2sf11CircleShapeE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text$_ZN2sf11CircleShapeD1Ev[_ZN2sf11CircleShapeD1Ev]+0x21): undefined reference to `__imp__ZTVN2sf11CircleShapeE'
C:\Users\ezj21\AppData\Local\Temp\cc861KBj.o:main.cpp:(.text$_ZN2sf11CircleShapeD1Ev[_ZN2sf11CircleShapeD1Ev]+0x3b): undefined reference to `__imp__ZN2sf5ShapeD2Ev'
collect2.exe: error: ld returned 1 exit status

I don't know why I'm having so much trouble
Title: Re: Installing SFML for VS Code
Post by: sykestech on January 24, 2020, 09:54:44 am
You aren't linking to the SFML libraries in that command. You need to use -L{SFML LIBRARY PATH} and -lsfml-graphics-d.lib -lsfml-network-d.lib etc, etc so it loads those libs into it.

You'll also need to copy the dll's from the sfml bin folder to the output of your compiled code (i.e. where the exe is), or statically link to those libraries using the -s command and setting the SFML_STATIC preprocessor command.

- On a note, I would probably look at some tutorials around compilers and how to use them, etc, this will help you in getting comfortable and understanding how to setup libraries. Alternatively, as I said in my previous post, use an IDE like Visual Studio/Code::Blocks as this will do a lot of the leg work for you, especially if you are inexperienced with such things.
Title: Re: Installing SFML for VS Code
Post by: 8Observer8 on May 01, 2020, 04:03:49 am
After a few experiments this command works:

g++ main.cpp -I"E:\Libs\SFML\SFML-2.5.1-windows-gcc-7.3.0-mingw-32-bit\SFML-2.5.1\include" -L"E:\Libs\SFML\SFML-2.5.1-windows-gcc-7.3.0-mingw-32-bit\SFML-2.5.1\lib" -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-system -o hero.exe

Copy DLL's like "sfml-system-2.dll", "sfml-graphics-2.dll" and so on to the "C:\Windows\SysWOW64" folder (for 64bit system) or to "C:\Windows\System32" (for 32bit system). It is good to copy them when you develop because you do not need to copy them every time and they will not spend your hard disk space.
Title: Re: Installing SFML for VS Code
Post by: rockmetal26 on August 15, 2020, 05:07:53 am
Hello, in this video I explain how to do the process

https://www.youtube.com/watch?v=VEn7PvNQ_Ho