Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to use SFML with MinGW in the simplest way possible  (Read 13197 times)

0 Members and 1 Guest are viewing this topic.

Argus

  • Newbie
  • *
  • Posts: 13
    • View Profile
How to use SFML with MinGW in the simplest way possible
« on: February 04, 2017, 11:41:09 am »
Hi everyone.

I am currently using very simple tools for learning C++ on Windows.
I'm writing my code in either of vim or sublime text, and compiling in command line using GCC/g++.

I would like to set up SFML so I can play with graphical elements, but when I search as to how to set up SFML, all of the topics I get are along the lines of "how to set up SFML for Visual Studio," "How to set up SFML in Code::Blocks", or instructions on how to compile the source code using Cmake to set up Static linking.

My question is, do I really need to do all this? Can I just copy the appropriate DLLs into my project folder, and #include them in my source code? Are there any dependencies that are not included in the DLLs that I would have to include somehow? My Google-fu seems to be kind of failing me.

*Note: I also asked this question on the cplusplus.com forums, but I'm reposting it here as I figure it may be a more appropriate place to ask.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to use SFML with MinGW in the simplest way possible
« Reply #1 on: February 04, 2017, 02:14:01 pm »
Hi everyone.
Hi!

My question is, do I really need to do all this? Can I just copy the appropriate DLLs into my project folder, and #include them in my source code?
Those set-up tutorials explain how to link the libraries using given IDEs so that they can be included.
The DLLs are for run-time use, not for build-/compile-time. The built executable uses these DLLs during its execution.
SFML's headers can be included from wherever they are, yes, but the actual built libraries (.lib or .o files, I believe) will also need to be linked so that they can be used during the build.
Again, how to do this is explained in the tutorials; that's what they are for :)
(They are very short really!)

However, since you won't be using an IDE (right? you're only using text editors and command line?), you will have to link manually.
It's likely you will need to build SFML for yourself too; the pre-built libraries need to match compilers exactly.

At a guess, the linux tutorial might be closest to what you need.

Are there any dependencies that are not included in the DLLs that I would have to include somehow?
Again, when building, it's not really the DLLs that are used. However, since you are dynamically linking (using DLLs), you should not need to link to anything just to use SFML. Note that SFML's audio module requires openal32.dll (provided with SFML) too.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Verra

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: How to use SFML with MinGW in the simplest way possible
« Reply #2 on: February 07, 2017, 06:05:52 am »
I buy a lot of spare computers for kicks, like 2 a year. I wrote a tutorial page for how to setup mingw/sfml a long time ago. http://verra.xyz/howto/sfml.html The tutorial is meant to be pretty cut and dry and I've never had trouble when I'm installing my environment on a new computer as long as I follow what I wrote there.

Argus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to use SFML with MinGW in the simplest way possible
« Reply #3 on: February 07, 2017, 03:44:55 pm »
Well, I broke down and decided to try following the Code::Blocks tutorial.
I am using the correct versions of GCC and SFML (both 4.9.2). I've spent about a dozen hours following that tutorial to a T, and am still getting errors (granted, different ones depending on whether I'm static or dynamically linking). It assumes some prior knowledge and leaves some things entirely unexplained, such as where to find / how to link to opengl32, winmm and gdi32, and where to put those files in relation to the sfml libraries.

I'll post more on this in another thread if necessary, but I may ditch Code::Blocks and take a look at Cmake.

This is a supremely frustrating experience. I would normally solve this kind of thing by relentlessly googling and trying things all day every day until I finally get it working, but I simply don't have that kind of time now =/

Maybe I'll try some video tutorials. Those tend to be far more useful as you can actually see the user go through the process step by step...
« Last Edit: February 07, 2017, 03:50:17 pm by Argus »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to use SFML with MinGW in the simplest way possible
« Reply #4 on: February 07, 2017, 05:40:10 pm »
It assumes some prior knowledge and leaves some things entirely unexplained, such as where to find / how to link to opengl32, winmm and gdi32, and where to put those files in relation to the sfml libraries.
Those files are Windows system files and as such are stored in \Windows\System32
However, note that these must not be moved and do not need to be copied.

I don't know Code::Blocks but it looks like you should add those libraries to the list in "Linker Settings" (along with sfml-graphics, sfml-windows and sfml-system, for example)

Don't forget to add SFML_STATIC to the "#defines"!

All of that is only if you are linking statically. Dynamically, the common problem is missing DLLs - they need to be next to the built executable.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: How to use SFML with MinGW in the simplest way possible
« Reply #5 on: February 07, 2017, 06:42:03 pm »
getting SFML setup on codeblocks is ridiculously easy

http://en.sfml-dev.org/forums/index.php?topic=21243.msg151540#msg151540

"leaves some things entirely unexplained, such as where to find / how to link to opengl32, winmm and gdi32, and where to put those files in relation to the sfml libraries."

codeblocks links all those things for you automatically
you don't have to touch them, move them or even think about them

Argus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to use SFML with MinGW in the simplest way possible
« Reply #6 on: February 07, 2017, 11:45:54 pm »
It assumes some prior knowledge and leaves some things entirely unexplained, such as where to find / how to link to opengl32, winmm and gdi32, and where to put those files in relation to the sfml libraries.
Those files are Windows system files and as such are stored in \Windows\System32
However, note that these must not be moved and do not need to be copied.

I don't know Code::Blocks but it looks like you should add those libraries to the list in "Linker Settings" (along with sfml-graphics, sfml-windows and sfml-system, for example)

Don't forget to add SFML_STATIC to the "#defines"!

All of that is only if you are linking statically. Dynamically, the common problem is missing DLLs - they need to be next to the built executable.

Wow. With regards to dynamic linking, that did it for me! Thanks! You may want to add that to the tutorial ;)
The only mention of the dll files in the entire tutorial is here in passing:
(click to show/hide)
I still get errors when attempting static linking however:

(click to show/hide)

I did all of the things you mentioned in regards to static linking, including setting the flag.


Edit: Hapax may have actually given me the very answer I was looking for. In regards to dynamic linking, if I want to setup another project outside of codeblocks, do I *only* need the dlls in the folder with my exe? Do I need any make files? I don't have to mess with linking settings or search directories or anything else? If that is the case, I don't understand the nature of the include statement in the example:
Code: [Select]
#include <SFML/Graphics.hpp>
« Last Edit: February 08, 2017, 12:04:12 am by Argus »

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: How to use SFML with MinGW in the simplest way possible
« Reply #7 on: February 08, 2017, 12:03:48 am »
for static debug you need this file
libsfml-graphics-s-d.a

of course you link it like this
sfml-graphics-s-d

for static release you need this file
libsfml-graphics-s.a

and you link it like this
sfml-graphics-s

same with all the other sfml files

edit:

you always need this
#include <SFML/Graphics.hpp>

your program uses certain sfml functions, methods and classes

your program needs to know what those functions, methods and classes look like 
what parameters do they take ? what do they return ?
an integer ? a float ?

the compiler needs to look up these definitions
so it looks in a .hpp or .h file

otherwise it would have no idea what an sf::RenderWindow is

so this line
#include <SFML/Graphics.hpp>

literally does a copy of this code
https://github.com/SFML/SFML/blob/master/include/SFML/Graphics.hpp
and pastes it into your file

now your compiler knows what each function, method and class looks like 

from my noob understanding linking is like giving your compiler an address book

the lib (or .a file) tells your compiler the address of the functions and methods you're using

so the .hpp gives it a definition
then the dll gives it the actual code
but its just one huge block of code, so the linking tells the compiler to use an import library to look up the address of each function in the dll (or in the static lib)

http://stackoverflow.com/questions/913691/dll-and-lib-files-what-and-why

https://www.quora.com/Why-do-I-need-a-LIB-file-for-my-C++-linker


so EVERY time you build an sfml program you must include and link and tell your compiler the location of everything

like the commands here show you
http://www.sfml-dev.org/tutorials/2.4/start-linux.php

or do it in codeblocks or visual studio (but you still have to do it every time)

but now once your program is running it needs the sfml code (if it was compiled dynamically) 
so you put the dlls somewhere in your path or in the same folder as your .exe
« Last Edit: February 08, 2017, 12:32:08 am by jamesL »

Argus

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: How to use SFML with MinGW in the simplest way possible
« Reply #8 on: February 08, 2017, 12:06:56 am »
for static debug you need this file
libsfml-graphics-s-d.a

of course you link it like this
sfml-graphics-s-d

for static release you need this file
libsfml-graphics-s.a

and you link it like this
sfml-graphics-s

same with all the other sfml files

I tried that, but it didn't seem to work. Thanks, though.
Do I need to point to the bin folder with the dlls in some kind of manner which is not mentioned in the tutorial?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to use SFML with MinGW in the simplest way possible
« Reply #9 on: February 11, 2017, 01:52:49 am »
Wow. With regards to dynamic linking, that did it for me! Thanks!
Hapax may have actually given me the very answer I was looking for.
You're welcome. Glad to help.

You may want to add that to the tutorial ;)
The only mention of the dll files in the entire tutorial is here in passing: "The settings shown here will result in your application being linked to the dynamic version of SFML, the one that needs the DLL files."
In the Code::Blocks tutorial, the paragraph above the green circle screenshot reads the following:
Quote
Compile it, and if you linked to the dynamic version of SFML, don't forget to copy the SFML DLLs (they are in <sfml-install-path/bin>) to the directory where your compiled executable is.
;)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*