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

Author Topic: How to compile SFML programs in c++11 ?  (Read 4505 times)

0 Members and 1 Guest are viewing this topic.

kishy nivas

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
How to compile SFML programs in c++11 ?
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How to compile SFML programs in c++11 ?
« Reply #1 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).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ruskointhehizzy

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to compile SFML programs in c++11 ?
« Reply #2 on: June 24, 2017, 08:19:13 am »
What ide are you using? Are you using codeblocks?

kishy nivas

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: How to compile SFML programs in c++11 ?
« Reply #3 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 .

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: How to compile SFML programs in c++11 ?
« Reply #4 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.

kishy nivas

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: How to compile SFML programs in c++11 ?
« Reply #5 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)

kishy nivas

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: How to compile SFML programs in c++11 ?
« Reply #6 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

 

anything