Skip to main content
GameDev.net gamedev.net
🔒 Locked

SFML - Can't even compile tutorial code

Started by Taint Feb 19, 2014 at 10:53 AM 12 replies 16.5k views
Original Post
Taint
Taint

I want to learn SFML and I've tried loads of different tutorials but I just get errors with everything I put into it, whether I copy it and type it or if I paste it.

My code:


#include <SFML/Window.hpp>

int main()
{
	sf::Window window;
	window.create(sf::VideoMode(640,480), "Window");
	while(window.isOpen())
	{
		sf:Event event;
		while(window.pollEvent(event))
		{
			if(event.type = sf::Event::Closed)
			window.close();
		}
	}
	return 0;
}

Errors:


g++ -c main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:9: error: ‘class sf::Window’ has no member named ‘create’
  window.create(sf::VideoMode(640,480), "Window");
         ^
main.cpp:7:15: error: ‘class sf::Window’ has no member named ‘isOpen’
  while(window.isOpen())
               ^
main.cpp:9:6: error: ‘Event’ was not declared in this scope
   sf:Event event;
      ^
main.cpp:9:6: note: suggested alternative:
In file included from /usr/include/SFML/Window.hpp:34:0,
                 from main.cpp:1:
/usr/include/SFML/Window/Event.hpp:197:7: note:   ‘sf::Event’
 class Event
       ^
main.cpp:9:12: error: expected ‘;’ before ‘event’
   sf:Event event;
            ^
main.cpp:10:16: error: ‘class sf::Window’ has no member named ‘pollEvent’
   while(window.pollEvent(event))
                ^
main.cpp:10:26: error: ‘event’ was not declared in this scope
   while(window.pollEvent(event))
                          ^
main.cpp:13:11: error: ‘class sf::Window’ has no member named ‘close’
    window.close();
           ^

Have I installed it wrong or am I missing something?

LaboPie
LaboPie

It seems that the compiler doesn't find the function, are you sure that you are linking the SFML libraries properly?

AbandonedAccount
AbandonedAccount

What version of SFML headers are you using? Those look like the errors you'd get if you were using the old 1.x headers where method names started with a capital letter. If so, just download a current version of SFML (2.1?) and you should be fine.

Taint
Taint

Ah, turns out I was using 1.6. Thanks!

Edit: I installed 2.1 using cmake and now I'm getting this error.


g++ main.cpp -lsfml-audio -lsfml-network -lsfml-graphics -lsfml-window -lsfml-system
main.cpp:1:27: fatal error: SFML/Window.hpp: No such file or directory
 #include <SFML/Window.hpp>

I really don't have the faintest idea what I'm doing, is there any reason installing this is so ridiculously difficult?

renega_666
renega_666

Did you remove SFML 1.6 before building SFML 2.0 with cmake? Did you run *make install* ? What is the output of *whereis libsfml-window*? (Assuming you're on linux)

Taint
Taint

Try simply following the official tutorials: http://www.sfml-dev.org/tutorials/2.1/

I did, exactly.

Did you remove SFML 1.6 before building SFML 2.0 with cmake? Did you run *make install* ? What is the output of *whereis libsfml-window*? (Assuming you're on linux)

Yeah I did, and I running "make install" seemed to do it. Now I'm getting yet another error. I can now compile my code, but can't run it.


./main
./main: error while loading shared libraries: libsfml-window.so.2: cannot open shared object file: No such file or directory
jHaskell
jHaskell

Ah, turns out I was using 1.6. Thanks!

Edit: I installed 2.1 using cmake and now I'm getting this error.


g++ main.cpp -lsfml-audio -lsfml-network -lsfml-graphics -lsfml-window -lsfml-system
main.cpp:1:27: fatal error: SFML/Window.hpp: No such file or directory
 #include <SFML/Window.hpp>

I really don't have the faintest idea what I'm doing, is there any reason installing this is so ridiculously difficult?

It's telling you it doesn't know where that header file is. You need to add a -L to your compile line so it knows where Window.hpp is. You have to provide paths to all such external headers and libs.

renega_666
renega_666

Yeah I did, and I running "make install" seemed to do it. Now I'm getting yet another error. I can now compile my code, but can't run it.

./main
./main: error while loading shared libraries: libsfml-window.so.2: cannot open shared object file: No such file or directory

May I ask you to open a terminal, run the following commands and paste their output here?

1) whereis libsfml-window

2) ldconfig -v 2>/dev/null | grep -v ^$'\t'

Edit: the error is probably that the sfml shared object ended up in a directory that is not in your ld path.

Taint
Taint

tom@tom-M52LT-D3 ~ $ whereis libsfml-window
libsfml-window: /usr/local/lib/libsfml-window.so
tom@tom-M52LT-D3 ~ $ ldconfig -v 2>/dev/null | grep -v ^$'\t'
/usr/lib/i386-linux-gnu/mesa:
/lib/i386-linux-gnu:
/usr/lib/i386-linux-gnu:
/usr/local/lib:
/usr/lib/nvidia-settings-319:
/lib/x86_64-linux-gnu:
/usr/lib/x86_64-linux-gnu:
/usr/lib/x86_64-linux-gnu/mesa-egl:
/usr/lib/x86_64-linux-gnu/mesa:
/lib:
/usr/lib:

DejaimeNeto
DejaimeNeto

You probably need to include the SFML directory.
Something like this:


g++ -c main.cpp -I~SFML_INCLUDE_DIRECTORY_GOES_HERE~
//like in
g++ -c main.cpp -I/home/me/SFML/include

I'd personally use an IDE.

@edit

Yeah I did, and I running "make install" seemed to do it. Now I'm getting yet another error. I can now compile my code, but can't run it.


./main
./main: error while loading shared libraries: libsfml-window.so.2: cannot open shared object file: No such file or directory

If g++ is building your program successfully, but you can't find it, look if the main executable wasn't generated in a different directory. Using -o to specify a name, as in g++ main.cpp -o program.exe can help you locate it.

But this may be a linkage problem; linking statically will probably solve this.

AbandonedAccount
AbandonedAccount

The only other thing I can think of is maybe you would be missing the symbolic link from .so.2 to .so.2.1 somehow.

Edit: removed bit about ldconfig since you weren't grepping the file, so that shouldn't be the problem.

BeerNutts
BeerNutts

Ah, turns out I was using 1.6. Thanks!

Edit: I installed 2.1 using cmake and now I'm getting this error.


g++ main.cpp -lsfml-audio -lsfml-network -lsfml-graphics -lsfml-window -lsfml-system
main.cpp:1:27: fatal error: SFML/Window.hpp: No such file or directory
 #include <SFML/Window.hpp>

I really don't have the faintest idea what I'm doing, is there any reason installing this is so ridiculously difficult?

It's telling you it doesn't know where that header file is. You need to add a -L to your compile line so it knows where Window.hpp is. You have to provide paths to all such external headers and libs.

Not -L, -I (capital i).

-L is for specifying library paths.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
DejaimeNeto
DejaimeNeto




./main: error while loading shared libraries: libsfml-window.so.2: cannot open shared object file: No such file or directory




Not -L, -I (capital i).

-L is for specifying library paths.

He has already fixed the include path problem. It looks like he just didn't copy the necessary files for the shared linking.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.