Log in

No account? Create an account

Using SFML and Eclipse C++ Development Tool (CDT) - Raz's Domain

Aug. 6th, 2009

12:22 pm - Using SFML and Eclipse C++ Development Tool (CDT)

Previous Entry Share Next Entry

There is a woeful dearth of tutorials for a variety of things you can do with SFML in general, and there are a woeful dearth of helpful guides for Eclipse's CDT.  This is an attempt to plug at least two holes with one stone... if that clumsy modification to the metaphor can work.

The specific Eclipse version is Galileo and the SFML version is 1.5, but the process will probably not change significantly if you are using an older or newer version of either of these.  This tutorial is also presented primarily for a Windows audience, but Eclipse runs the same (as I understand it) in graphical Linux exactly the same way, so the only real difference should be the directories shown in screenshots.

Installing SFML

You install SFML like you would with any other IDE.  Download the appropriate development files or the whole SDK for your development environment, and extract the files to the directory of your choice.  The SFML Development Files have been set up for a variety of operating systems and with some particular IDEs in mind already.

Once you've got the files on your hard drive, you need to tell Eclipse where to look for them.

Setup Eclipse CDT so it Can Find the Headers Libraries

  • Go to Project menu (or right click your project directly), and choose "Properties"
  • Expand C/C Build (click the plus), and select "Settings"
  • Under GCC C Compiler select "Directories" and click "Add..." and find the SFML Include directory (example: C:\SFML-1.5-sdk\SFML-1.5\include).  Click OK
  • Under MinGW C Linker, click Libraries and click "Add..." next to Library Search Path.  Find the SFML library directory (example: C:\SFML-1.5-sdk\SFML-1.5\lib\mingw).  Click OK.
  • Expand C/C General, select Path and Symbols.
  • On the Includes tab, select GNU C and make sure the include directory is listed.  If not, added it manually.  It should be there already!
Directory Setting Picture...
Directories Pic
Linker setting picture....
Linker Pic

Compiling Your First Program

If you've gotten this far, you've got your new C Project created and waiting for some files.  Time to make a quick program to see that everything is working!

Create a new Source file and fill it with the following code...

#include <SFML/System.hpp>
#include <iostream>

int main()
    sf::Clock Clock;
    while (Clock.GetElapsedTime() < 5.f)
        std::cout << Clock.GetElapsedTime() << std::endl;

    return 0;

Before you compile using a library you need to be sure to set the libraries appropriately in your Project's linker settings.  We are using System.hpp, so we need to be sure the linker looks for the sfml-system library.

Go to your project properties again, and return to the linker settings as described earlier.  Click "Add..." next to libraries and type the library we want.  In this case, it will be sfml-system.

Now this small program should compile and run!

Note about libraries....

For more complicated projects, the library setting can be the root cause of many problems when you are attempting to build your project.  Order matters, and there is a bit of a heiarchy to the libraries; System is at the top and includes everything else. 

If you include Graphics.cpp or Window.cpp, you're going to need to include the sfml-graphics and sfml-window libraries.  It might vary depending on what linker you use (MinGW's gcc linker is Eclipse CDT standard), but generally you will have those two at the top of the list, and have sfml-system very last.

The libraries come in a variety of flavors.  The default one relies on .DLLs, but if you want to rely on the static versions make sure to include -s at the end of the library name.  Debug versions should run using the libraries with -d appended to the end because behavior will is undefined and errors might occur.  For example, sfml-window-s or sfml-window-s-d. 

If you use the dynamic libraries, you must place the DLLs in your executable's folder or to a directory in the PATH environmental variable.

You'll also, if you link to the dynamic libraries, need to define a preprocessor macro: SFML_DYNAMIC

If you are using the Audio package, you must also copy the DLLs of the external libraries needed by it, which are libsndfile-1.dll, and OpenAL32.dll.
These files can be found in the extlibs\bin directory of the package that you downloaded (SDK or development files).

All of these settings can be set differently for your Build and Debug configurations, so if you're switching your configuration make sure you update your properties for the new build!