<< Back

How to Install SFML in Code::Blocks for Windows

On This Page

  1. Introduction
  2. Installing SFML
  3. Creating an SFML Project
  4. Exiting and Restarting Projects
  5. More Information
  6. In Case of Trouble

A. Introduction

This document describes how to install SFML in Code::Blocks on a computer for the course CS-11: Introduction to Programming Concepts and Methodology, C++. SFML is a free and open source library that provides a simple object-oriented interface to graphics, sound and other parts of a personal computer. It has five main modules: system, window, graphics, audio and network. Since this is an introductory course, not all options are covered such as static builds.

These instructions assume that you have already installed Code::Blocks as described in the document: How to Install Code::Blocks on Windows. If not, do so now.

For Linux computers, SFML installation is described in How to Install Code::Blocks and SFML for Linux. For Macintosh computers, SFML installation is described in How to Install SFML in OS-X.

B. Installing SFML

Follow these instructions carefully. Do NOT just continue without completely reading each step of the instructions carefully or you will probably not install the software correctly. If in doubt ask a classmate or the instructor to verify what you are doing.

If you cannot create an SFML project after installing, then you will need to reinstall Code::Blocks, SFML or both.

  1. In your CodeBlocks folder, create a new folder named "sfml" (without the quote marks). You will install the SFML files into this folder.

    Folder sfml

  2. Go to the SFML download page and follow the link to the latest stable version.
  3. Download the binaries corresponding to your compiler (GCC), 32-bit words and variant (SJLJ vs DW2) with SJLJ preferred, like: GCC 4.9.2 TDM (SJLJ) - 32 bits.

    If unsure, check which of the files libgcc_s_sjlj-1.dll or libgcc_s_dw2-1.dll you have in your MinGW/bin folder. If you have both, choose the SJLJ variant of SFML. The binary for the classroom computers is: GCC 4.8.1 TDM (SJLJ) - 32 bits. Here is the usual file to select:

    Usual SFML file: GCC 4.9.2 TDM (SJLJ) - 32 bits.

  4. Unzip the downloaded file (see Windows unzip) and move (not copy) all the extracted SFML files and folders to the CodeBlocks folder named "sfml".

    Make sure you have the correct folder structure. Do NOT have any extra file folders like "SFML 2.4.2" in the file path.

    Folders and files of SFML

  5. Start CodeBlocks and click on the Settings->Compiler menu to open the Compiler settings dialog.
  6. In the Compiler settings dialog:
    1. In the left-hand menu, verify Global compiler settings is selected,
    2. Select the Toolchain executables tab.
    3. Select the Additional Paths tab.
    4. Press the Add button, find the bin folder of SFML (C:\CodeBlocks\sfml\bin), and add it without keeping relative paths.
    5. Press the OK button at the bottom of the dialog.

    Adding Toolchain executable path

    If you are reinstalling Code::Blocks, you may get a message, "Path already in the extra paths list!" If so, click OK and continue.

C. Creating an SFML Project

In this section we create a test project to verify the SFML installation. We can use these instructions as a starting point for other new projects as well.

  1. Run Code::Blocks and click the "Create a new project" link or follow the menus: File -> New -> Project....

    Starting a Code::Blocks project.

  2. Select the "Empty project" wizard by double-clicking the Empty project icon shown.

    Starting a Code::Blocks project.

  3. In the "Empty project" start screen press the Next button.

    Empty project wizard starts

  4. In the following window, enter a project title and folder path and then press the Next button. The example project is "test-sfml".

    Example project test-sfml

  5. Continuing through the "Empty project" wizard, verify the GNU GCC compiler is selected and then press the Finish button.

    Verify compiler

  6. In the Project menu, select Build options.... (If "Build options" is grayed out, check to see if another Code::Blocks window is hidden behind the main window.) After selecting Build options... then:
    1. Click the global project name (like test-sfml) on the left-hand menu. The project name is what you entered in step 4.
    2. Select the Search directories tab.
    3. Select the Compiler subtab.
    4. Press the Add button, find the include folder of SFML, and add it without keeping relative paths. If you installed SFML to the correct folder, then you can use (C:\CodeBlocks\sfml\include) independant of the title of your project.

    Verify compiler

  7. Similarly, set the Linker path to the lib folder (C:\CodeBlocks\sfml\lib) by:
    1. Clicking the global project configuration (project name) on the left-hand menu.
    2. Selecting the Search directories tab.
    3. Selecting the Linker subtab.
    4. Pressing the Add button, finding the lib folder of SFML (C:\CodeBlocks\sfml\lib), and adding it without keeping relative paths.

    Verify compiler

  8. Also in the Project build options dialog:
    1. Click the global project configuration (project name) on the left-hand menu,
    2. Select the Linker settings tab,
    3. Press the Add button and add the Link libraries in the order shown by typing the following names into the Add library dialog box:
       sfml-graphics
       sfml-window
       sfml-system
      
      Typing in libraries Do NOT find a file. Instead, type (or copy) in the names shown to the left one at a time and press the OK button in the "Add library" dialog between each added name.

    The GCC compiler requires the link libraries to be in the correct order as shown. If you add sfml-audio or sfml-network, add them before sfml-window.

    Verify compiler

  9. When finished adding the project build options, press the OK button of the "Project build options" dialog shown above.
  10. Add a file to the project by using the menus File->New->Empty file and choosing an appropriate file name (test.cpp).

    Read the dialogs and select appropriately. If in doubt, use my suggestions like test.cpp.

  11. In the file tab (test.cpp), copy and paste the following program code.
    #include <SFML/Graphics.hpp>
    
    int main()
    {
        sf::RenderWindow win(sf::VideoMode(200, 200), "SFML Test");
        sf::CircleShape shape(100.f);
        shape.setFillColor(sf::Color::Green);
    
        while (win.isOpen())
        {
            sf::Event event;
            while (win.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                {
                    win.close();
                }
            }
    
            win.clear();
            win.draw(shape);
            win.display();
        }
    
        return 0;
    }
    
  12. Build the project by clicking on the Build icon Code::Blocks build icon in the toolbar, using the menus Build->Build, or pressing Ctrl+F9.

    Code::Blocks Build icon

  13. When you build, you will see a pane at the bottom of Code::Blocks called Build Log. You should see an output like this showing the compiler did not report any errors:

    Code::Blocks Build Log

    If you have problems, check the In Case of Trouble section below.

  14. Run the program by clicking on the Run icon Code::Blocks run icon in the toolbar, using the menus Build->Run, or pressing Ctrl+F10, and verify you see the following graphics window.

    If you have problems, check the In Case of Trouble section below.

    Verify test graphics

  15. After closing the graphics window by pressing the red X, verify the message process returned 0 appears in the console.

    Verify correct ending

  16. Click on the console and press the space key to close the console window.

D. Exiting and Restarting Projects

E. More Information

F. In Case of Trouble

  1. If you see an error message like:
    unknown type name 'namespace'
    

    verify that the file extension is .cpp and is not a .c file extension.

  2. If you see an error message like:
    The procedure entry point ___ could not be located in the dynamic link library
    

    make sure you have the correct SFML binary. (See Installing SFML step 3)

  3. An error message like:
    Process terminated with status -1073741510
    

    means you closed the console window before closing the SFML graphics window. To prevent the message, close the SFML graphics window before closing the console (terminal) window.

  4. An error message like:
    Failed to share the OpenGL context
    

    means that SFML could not correctly set up OpenGL because the window is larger than the screen or other reasons such as using remote desktop.

  5. An error message like:
    fatal error: SFML/Graphics.hpp: No such file or directory
    

    means that SFML could not locate the SFML/Graphics.hpp file because it is installed in the wrong folder. See Installing SFML steps 1 and 4.

  6. If the "Build" option is grayed out, close any SFML and Terminal windows started by Code::Blocks.

For any other error, try reinstalling Code::Blocks, SFML or both, following the instructions carefully. Let the instructor know about your error and how you corrected the problem.

<< Back