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

Author Topic: Getting SFML working on Ubuntu based Linux  (Read 8139 times)

0 Members and 2 Guests are viewing this topic.

Jaims_DC

  • Newbie
  • *
  • Posts: 3
    • View Profile
Getting SFML working on Ubuntu based Linux
« on: August 24, 2013, 01:32:24 am »
Hi everyone, I'm sure similar questions have been asked but my issue is probably very straight forward for those in the know, and I figure I'll probably just confuse things even more if I try and adapt other peoples solutions considering I'm not 100% confident about tackling the issue (hence the reason I am here).

I'm running ElementaryOS Luna (Ubuntu based) running kernel 3.8.

I have gcc version 4.6.3 installed, and as expected it works perfectly fine with Netbeans with respect to compiling C++ programs.

I'm following this tutorial: http://www.sfml-dev.org/tutorials/2.1/start-linux.php.
So far I have run sudo apt-get install libsfml-dev.
No problems there.

I copied the following code and saved it to a .cpp file :

#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;
}

I then ran g++ -c main.cpp and it compiled fine.

Next up I run g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system  as per the tutorial linked above. That's when it all goes wrong, terminal output is as follows :

james@LunaBook:~$ cd NetBeansProjects/
james@LunaBook:~/NetBeansProjects$ g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
main.o: In function `main':
main.cpp:(.text+0xeb): undefined reference to `sf::String::String(char const*, std::locale const&)'
main.cpp:(.text+0x12d): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
main.cpp:(.text+0x161): undefined reference to `sf::CircleShape::CircleShape(float, unsigned int)'
main.cpp:(.text+0x175): undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
main.cpp:(.text+0x190): undefined reference to `sf::Window::close()'
main.cpp:(.text+0x1a6): undefined reference to `sf::Window::pollEvent(sf::Event&)'
main.cpp:(.text+0x1e5): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
main.cpp:(.text+0x1fc): undefined reference to `sf::RenderStates::Default'
main.cpp:(.text+0x207): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
main.cpp:(.text+0x216): undefined reference to `sf::Window::display()'
main.cpp:(.text+0x225): undefined reference to `sf::Window::isOpen() const'
main.o: In function `sf::CircleShape::~CircleShape()':
main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x13): undefined reference to `vtable for sf::CircleShape'
main.cpp:(.text._ZN2sf11CircleShapeD2Ev[_ZN2sf11CircleShapeD5Ev]+0x1f): undefined reference to `vtable for sf::CircleShape'
collect2: ld returned 1 exit status
james@LunaBook:~/NetBeansProjects$
 

So I think there is some issue with things not being visible to the linker?

If someone could explain to me how I can fix this problem and linker problems in general it would be greatly appreciated.

Thanks in advance.


Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Getting SFML working on Ubuntu based Linux
« Reply #1 on: August 24, 2013, 02:14:09 am »
Since you said you used sudo apt-get install libsfml-dev, here's my guess:

When I tried to get SFML running on Ubuntu I noticed the sfml package they provide is not exactly up to date, though it isn't labeled with the version number so it's very hard to tell.  If you want SFML 2.1 (which you should), I think you have to rebuild it from source like I did.  The good news is CMake makes that process a lot easier than I expected it to be.

If that's not it then I have no idea.

Jaims_DC

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Getting SFML working on Ubuntu based Linux
« Reply #2 on: August 24, 2013, 03:57:42 am »
Thanks for pointing me in the rite direction.
I followed this tutorial:
http://www.sfml-dev.org/tutorials/2.1/compile-with-cmake.php

It was a bit intimidating at first but I got there step by step.
I'd suggest for other newcomers attempting this install, synaptic package manager if you haven't got it already :

sudo apt-get install synaptic

It makes finding the correct packages actually possible!

Also installing the gui cmake tool as suggested in the tutorial makes the terminal a lot less cluttered :

sudo apt-get install cmake-qt-gui

I think that's about it, I hope this can help others  :)

Pap

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • Email
Re: Getting SFML working on Ubuntu based Linux
« Reply #3 on: August 24, 2013, 12:53:09 pm »
I wrote two simple scripts that facilitate installing/uninstalling SFML, and I post  them here in case someone else finds them useful (as I do.)
Once you installed the libraries needed (as described in http://www.sfml-dev.org/tutorials/2.1/compile-with-cmake.php, you can run this script to install SFML:

#!/bin/bash
separator=--------------------------------------------------------------------------------
install_prefix=/usr/local
zip_dir=/home/pap/Downloads
editor=emacs
echo Installing SFML \(install prefix: $install_prefix\).

echo $separator
echo \(1\) Unzipping SFML source file.
unzip -q $zip_dir/SFML-*-sources.zip -d $install_prefix/share

echo $separator
cd $install_prefix/share/SFML*
source_dir=$(pwd)
echo \(2\) Switching working directory $source_dir.

echo $separator
echo \(3\) Changing doxygen settings.
echo You need to modify $source_dir/doc/doxyfile.in so that the
echo SEARCHENGINE flag must be YES.
echo
read -p "Press any key to edit doxygen settings." -n1
$editor $source_dir/doc/doxyfile.in

echo $separator
echo \(4\) Running cmake.
echo You need to change flags SFML_BUILD_DOC and SFML_BUILD_EXAMPLES to TRUE.
echo
read -p "Press any key to run cmake." -n1
cmake -i

echo $separator
echo \(5\) Compiling SFML.
echo
read -p "Press any key to start compiling SFML." -n1
make

echo $separator
echo \(6\) Installing SFML.
echo
read -p "Press any key to install SFML." -n1
make install

echo $separator
doc_index=$install_prefix/share/SFML/doc/html/index.htm
echo \(7\) Fixing javascript issue in searchable documentation.
echo You need to add the following lines:
echo \<script type="text/javascript" src="search/search.js"\>\</script\>
echo \<link href="search/search.css" rel="stylesheet" type="text/css"/\>
echo to the header of the main documentation file $doc_index
echo
read -p "Press any key to edit documentation index file." -n1
$editor $doc_index

echo $separator
echo SFML installed.

The script takes care of instaling everything, including doxygen documentation and also lets you enable search engine (very useful). All you need to do is to change two parameters in the top of the script: zip_dir (where SFML source zipped file was downloaded) and editor (your preferred editor, which will be used to do the necessary changes in doxygen.in and documentation index.) You might also change install_prefix, in case you want to install SFML to a different place than the default.

To uninstall SFML, for example in order to install a newer version, use this script:

#!/bin/sh
separator=--------------------------------------------------------------------------------
install_prefix=/usr/local
echo Uninstalling SFML \(install prefix: $install_prefix\)
echo $separator
echo Removing directory $install_prefix/include/SFML
rm -f -r $install_prefix/include/SFML
echo Removing entries in $install_prefix/lib/
rm -f $install_prefix/lib/libsfml*
echo Removing entries in $install_prefix/lib/pkgconfig/
rm -f $install_prefix/lib/pkgconfig/sfml*
echo Removing directories in $install_prefix/share/
rm -f -r $install_prefix/share/SFML*
echo $separator
echo SFML uninstalled.

Again, you might need to change install_prefix, in case you installed SFML to a different place than the default.
Both scripts should work on any Linux distribution (not necessarily Ubuntu or other Debian-based distros.)
« Last Edit: August 24, 2013, 12:55:48 pm by Pap »