Leap from Console

Hey guys, new developer in C++. I have some experience in web work, such as working in PHP, but other than that, C++ is a new experience. The trouble is, it seems to be a large leap from doing work in the console to interacting with Windows or other entities in a meaningful way.

I have been through several "Beginner Series" and even read two books on C++, but they all worked exclusively in the console. That's wonderful, but I want to make a pretty picture pop up. Everyone speaks of SMFL, but upon downloading their SDK I was immediately way over my head.

Dependencies, having to have DLLs moved around, Linker errors up the wazoo, not sure where in Visual Studio to put everything, just a trainwreck of things that were never explained, and the documentation on SMFL's site seems to infer previous knowledge of said processes.

Where would one go to really get an easing from console work into, say, writing for Windows? If possible, I'd love to skip the preliminaries, as I'm well versed in pointers by now. But anything would help, I'm having trouble finding instruction in that middle ground that I'm at, trying to get out of the console.

Thank you in advance.
That's wonderful, but I want to make a pretty picture pop up. Everyone speaks of SMFL, but upon downloading their SDK I was immediately way over my head.


Getting it set up is the hardest part. Once it's installed it's probably the easiest way to go.

Admittedly the SFML tutorials are a little lacking... and this problem is surprisingly common for newbies. So common that I've decided to create this step-by-step tutorial to get it working with Visual Studio.

Note I know this seems like a lot of work but you only have to do it once, then SFML will work in every program with minimal effort forever.

Also note that I statically link SFML for simplicity, and also because dynamic linking on Windows is extremely problematic.


1) Get the latest SFML 2.0 snapshot. Don't bother with SFML 1.6. Even though it's still technically WIP, 2.0 is more stable and more functional than 1.6. The latest can be found here:

http://sfml-dev.org/download.php

Right ABOVE the "Official SFML Libraries" section, you'll see a link that says "SFML 2.0 snapshot". That's what you want.

2) Extract that file somewhere. If you don't have a program that can extract .tar.gz files... 7-Zip can do it and is free ( http://www.7-zip.org/ ).

Where you put the files does not matter as long as you know where they are. I try to keep all downloaded libs in the same directory to keep things somewhat organized.

3) Find the <root> directory. Once fully extracted, you should have several directories named "cmake", "doc", "examples", etc. Along with approx 3 text files... one of which is CMakeLists.txt. This is the <Root> directory. Make sure you find this directory and know where it is, as you'll need to refer to it later.

Any other directories above this one can be removed/renamed. The default names are crazy hashtags... you might want to rename them to just "SFML-2.0" or something.

But pick a name and stick with it now, as it will be difficult to change later.


4) Get CMake. http://www.cmake.org/cmake/resources/software.html#latest

Look at the "Binary distribution" section. Get either the Windows .zip or the Windows installer... whichever you prefer.

4.5) Get Doxygen. This is not required, but is highly recommended. Doxygen is a tool to generate documentation based on source code. This will give you a local copy of all the reference material for SFML. It will be extremely handy.

Doxygen can be found here: http://sourceforge.net/projects/doxygen/files/snapshots/

There should be a "Download doxygen-1.x-setup.exe" link -- that's what you want.


5) Run CMake. In the top-right corner, click the "Browse Source" button. Select the <root> directory I mentioned in step 3.

The <root> directory should now be shown in the "Where is the source code" box. Right below that box, there is a "Where to build the binaries" box. Copy/paste the root dir into that box and add "/projects" to it.

Example:

Where is the source code: "C:/a/b/c/d/root"
Where to build the binaries: "C:/a/b/c/d/root/projects"

6) Configure CMake.

On the center left of the CMake window there's a "Configure" button. Click it. It'll pop up a box saying the build directory doesn't exist. Say "yes" to create it.

Then it'll pop up another box asking what kind of project file you want to create. Since you're using Visual Studio, you'd want to select the version of Visual Studio you're using:

VS2012 = Visual Studio 11
VS2010 = Visual Studio 10
VS2008 = Visual Studio 9
older = update your copy of VS for crying out loud

Leave the "Use default native compilers" option selected, then click "Finish" at the bottom.
Last edited on

7) Configure SFML.

CMake will be doing a bunch of stuff. Once it finishes it should give you a list of options, such as:
"BUILD_SHARED_LIBS"
"CMAKE_BUILD_TYPE"
etc

Most of these can be left as the defaults, however we want to change some of them:

BUILD_SHARED_LIBS should be unchecked
SFML_USE_STATIC_STD_LIBS should be checked
SFML_BUILD_DOC should be checked --- BUT ONLY IF YOU HAVE DOXYGEN INSTALLED. If you skipped that, leave this unchecked.

This configures SFML so you do not have to worry about as many dll problems.

Once those options are configured... click the "Configure" button again.

The options should all turn white except for maybe a new one "DOXYGEN_HHC_PROGRAM" which might say something about a file not being found. Don't worry about that.

8) Build SFML

Click "Generate" in the CMake window. It'll do its thing. When it says "Generating done" in the status box, you can close CMake.

Then go to the <root>/projects folder. You should see a SFML.sln file. Open that in Visual Studio.

Do a build. You might get a bunch of warnings, but it should not give you any errors. At the end it should say "9 succeeded, 0 failed, 0 up-to-date, 1 skipped"

Switch the build configuration from Debug to Release. If you do not know how to do this, there should be a drop box on the toolbar that says "Debug". Just click and and switch it to "Release". If that drop box is not there, go to View -> Toolbars -> Standard.

Do another build now that you're in Release. Again, you might get warnings but no errors.

8.5) Make an "auto-link" header so you don't have to worry about linking to .libs every time you make a new project. This step is not necessary, but I recommend it because it saves a lot of hassle.

I put a paste on pastebin here: http://pastebin.com/0Kn67xjB

Copy that into a file... save it in the <root>/include directory as "sfml.h".

EDIT: I've also pasted the code further in this thread in case the pastebin link is broken. You can find it here:
http://cplusplus.com/forum/beginner/95295/#msg512212


9) Configure Visual Studio

This is the most annoying step, IMO. This used to be much, much simpler in VS but they "deprecated" the intuitive way to do it, now you're forced to do this roundabout BS:

Make a new project in Visual Studio. Be sure to select "Empty project". You don't want any of that stdafx crap. The name of this project does not matter.

Assuming you have VS2012, go to View -> Other Windows -> Property Manager.
I *think* it's the same in older versions of VS but I can't say for certain.

The property manager window should have a single item in it named "<whatever you named your project>", with a little drop arrow on the left. Click the drop arrow to expand it.

Once expanded you should see Debug and Release directories. Expand the Debug directory.

Right click on the "Microsoft.Cpp.Win32.user" item and select "Properties" from the popup menu.

On the new window, select the "VC++ Directories" entry from the list on the left.

On the right-half of the screen, click on "Include Directories". A drop arrow will appear on the very right-hand side. Click it and select "Edit" from the menu.

Another new window will pop up. It should have a "new folder" looking icon in the top-right. Click it. It will add a new line to the list below it, and it should have an elipsis (...) button on the right. Click that button.

Navigate to the <root>/include directory, then hit "Select Folder"

Click "OK" to get out of the "Include Directories" window.

Then click on the "Library Directories" item and do all the same thing, only instead of adding the include directory... add the "<root>/projects/lib/Debug" and "<root>/projects/lib/Release" directories.

Keep hitting ok until you close all the damn windows.


10) You're done!

You can now start using SFML in any program. Just #include <sfml.h> and you're good to go (ignore any tutorials that tell you to #include <SFML/Graphics.hpp> or any of those other headers... we're using the wrapper header to simplify it).


You will probably still get linker errors unless you statically link to the Std Runtime libs. If this happens... do the following:

- right click on your project from the solution explorer
- expand the C/C++ item on the left
- Select the "Code Generation" item on the left
- Look for the "Runtime Library" option on the right. It should be set to:

"Multi-threaded Debug (/MTd)" for Debug configurations
and
"Multi-threaded (/MT)" for Release configurations



I'm happy to answer Qs if you still have problems with it.
Last edited on
In archlinux
1
2
3
4
5
//installing sfml-2.0
# pacman --sync sfml

//To link
$(pkg-config --libs sfml-all)


¿Could you explain why did you choose static linking?
Last edited on
Wow, thanks Disch. I will have to try this out, I have one SFML project that magically works, probably a good idea to get it done right instead of copy/pasting that project when I want to make a new SFML program.

Is this process more or less suitable for any library?
ne555 wrote:
In archlinux [2 lines]


Yes this is one of the things (read: the only thing) I really liked about my time on Ubuntu. Installing and using additional programming libs was incredibly simple.

As much as I dislike *nix... that is one thing I really have to hand to it. It got that right.

ne555 wrote:
¿Could you explain why did you choose static linking?


Because it's about 100x simpler for newbies. They don't have to worry about placing dlls, and there are less dlls they have to distribute with their program when they give it to other people.

This is a guide for newbies to get set up and running. By the time a developer understands the difference between static and dynamic linking and can make an educated decision as to whether or not they feel dynamic linking is better, they will be able to revise the above steps on their own to make it dynamic.

But really, IMO, dynamic linking freaking blows on Windows. Especially with C++ libs. The few advantages to it are overshadowed by the additional problems it causes. Especially for such a small lib like SFML.


Is this process more or less suitable for any library?


Kinda sorta. The idea is the same, but other libs may or may not use CMake so getting them built might be a different process.
Hello, thank you so much for the reply, I got all the way through the steps with little problem, but there is an inconsistency on Step 9. I am running Visual Studio 2010.

Assuming you have VS2012, go to View -> Other Windows -> Property Manager.
I *think* it's the same in older versions of VS but I can't say for certain.

The property manager window should have a single item in it named "<whatever you named your project>", with a little drop arrow on the left. Click the drop arrow to expand it.


Under View -> Other Windows it has "Properties Window" but it doesn't seem to match the description of, or have the elements of, what you're describing. I poked around a bit but couldn't figure out where it was in 2010. That's the only part that needs addressing.

There is no "Debug" or "Release" properties to work with, unless I'm not seeing it, but there is only about 4 elements on that window.

Again, thank you so much.

[EDIT: I found it, it's hidden by default in VS2010. Go to Tools -> Settings -> Expert Settings then go to the View menu and Property Manager will be there.]
Last edited on
Pasting the sfml convenience header here just so all the info is in the same place. I'll probably be linking to this thread in the future and I wouldn't want the pastebin link to go bad.


This is <sfml.h> :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once
#ifndef SFMLFULL_INCLUDED
#define SFMLFULL_INCLUDED
 
#define SFML_STATIC
 
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
 
#if defined(_DEBUG) || defined(DEBUG)
    #pragma comment(lib,"sfml-graphics-s-d.lib")
    #pragma comment(lib,"sfml-audio-s-d.lib")
    #pragma comment(lib,"sfml-network-s-d.lib")
    #pragma comment(lib,"sfml-window-s-d.lib")
    #pragma comment(lib,"sfml-system-s-d.lib")
    #pragma comment(lib,"sfml-main-d.lib")
#else
    #pragma comment(lib,"sfml-graphics-s.lib")
    #pragma comment(lib,"sfml-audio-s.lib")
    #pragma comment(lib,"sfml-network-s.lib")
    #pragma comment(lib,"sfml-window-s.lib")
    #pragma comment(lib,"sfml-system-s.lib")
    #pragma comment(lib,"sfml-main.lib")
#endif
 
 
#endif // SFMLFULL_INCLUDED 
Going to bump this thread a little:

I made SFML 2.0 for Mingw32 on XP 32 bit and had some trouble. It may be because I'm not too sure what I did when I installed Mingw/MySys, but maybe it is for everyone. I also followed Disch's tutorial to the tee (except for the configuring), so maybe it's an issue there or with CMake...

Whatever, the problem is in "include/SFML/OpenGl.hpp near line 39. I got SFML to compile by commenting line 4 and 6 in the following:
1
2
3
4
5
6
7
8
9
10
11
#if defined(SFML_SYSTEM_WINDOWS)

    // The Visual C++ version of gl.h uses WINGDIAPI and APIENTRY but doesn't define them
    //#ifdef _MSC_VER
        #include <windows.h>
    //#endif
	
    #include <GL/gl.h>
    #include <GL/glu.h>

#elif defined(SFML_SYSTEM_LINUX) || defined(SFML_SYSTEM_FREEBSD) 


SFML documents imply that Window.hpp already includes the headers for OpenGL (which requires Windows.h on my computer), but it doesn't seem to be the case for me. To use OpenGL in my program my code needs to include the files specifically:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////

// STD c++ headers must be before OpenGL headers
// Not sure if the location of SFML headers matters
#include <iostream>

#include "windows.h"
#include "GL/GL.h"
#include "GL/GLU.h"

#include <SFML/Window.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RenderWindow.hpp> 


For those perhaps unsure about the command line compile:
g++ -I(your sfml include dir) test.cpp -o test.exe -DSFML_STATIC -L(your sfml lib dir) -lsfml-graphics-s -lsfml-window-s -lsfml-system-s -lopengl32 -lglu32
Last edited on
Topic archived. No new replies allowed.