SFML community forums

Help => General => Topic started by: kishy nivas on June 23, 2017, 09:53:17 pm

Title: How to compile SFML programs in c++11 ?
Post by: kishy nivas on June 23, 2017, 09:53:17 pm
When I try to compile with c++ 11,  it says undefined reference to SFML libraries ,

g++ -std=c++11 main.cpp

it works for other c++11 programs but not working for SFML programs
Title: Re: How to compile SFML programs in c++11 ?
Post by: eXpl0it3r on June 23, 2017, 11:29:54 pm
First, maybe take some time to think about where you want to post it. There's a dedicated Help section, yet you post it once in the Wiki section and the General Discussion forum. ;)

Next you need to learn some more how the (G++) compiler works. If you want to use a library, you have to link them with the -l flag. You can look up the man page for g++ (or gcc).
Title: Re: How to compile SFML programs in c++11 ?
Post by: Ruskointhehizzy on June 24, 2017, 08:19:13 am
What ide are you using? Are you using codeblocks?
Title: Re: How to compile SFML programs in c++11 ?
Post by: kishy nivas on June 24, 2017, 08:48:53 am
Codeblocks didn't work for me when I compile it with ordinary c++ -sfml program it says
error while loading shared libraries llbsfml-graphics.so.2 like that

But it works correctly on terminal ,

So I looked how to compile with external libraries and I did this in terminal :

 g++ -std=c++11  -I/home/kishore/SFML-2.4.2/include -c main.cpp -o main.o
which compiles with no problem

g++ main.o -o sfml-app -L/home/kishore/SFML-2.4.2/lib -lsfml-graphics -lsfml-window -lsfml-system
again no problem

but when i try to run ./sfml-app
I get the following error :

error while loading shared libraries: libsfml-graphics.so.2.4: cannot open shared object file: No such file or directory

The same one I got during codeblocks , What am I doing wrong ? any help would be appreciated .
Title: Re: How to compile SFML programs in c++11 ?
Post by: Mario on June 24, 2017, 08:52:20 am
You're doing nothing wrong, the program just can't find the shared libraries belonging to SFML (since they're probably not installed in a standard path).

Put SFML's *.so files next to your executable and set your "LD_LIBRARY_PATH" to point there, e.g. run it like this:

Code: [Select]
LD_LIBRARY_PATH=. ./myProgram
There are other ways to achieve this, but I guess some of the Linux guys here knows those better than me. :)

As an alternative, compile a static version of SFML and link that.
Title: Re: How to compile SFML programs in c++11 ?
Post by: kishy nivas on June 24, 2017, 09:06:49 am
Gee it worked, also how do  I link this to codeblocks, as it also emits the same error ( this is my first time using codeblocks too)
Title: Re: How to compile SFML programs in c++11 ?
Post by: kishy nivas on June 24, 2017, 09:18:05 am
I used this in terminal and it works and I can't ask for more, thanks a lot for helping me guys !!!!

 
g++ -std=c++11 -c main.cpp
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system
./sfml-app