Sign in to follow this  
Taint

SFML - Can't even compile tutorial code

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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?

Edited by Taint

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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<include_directory> to your compile line so it knows where Window.hpp is.  You have to provide paths to all such external headers and libs.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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:

Share this post


Link to post
Share on other sites

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.

Edited by dejaime

Share this post


Link to post
Share on other sites

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.

Edited by richardurich

Share this post


Link to post
Share on other sites

 

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<include_directory> 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.

Share this post


Link to post
Share on other sites


./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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this