3

I wanted to make gui apps in c++ and found SFML to be a good choice. Fortunately i am on linux so SFML(2.4) was already installed on my system. So i started searching for some tutorials and found one that makes a simple window. But when i run the code i get an error saying undefined reference to sf::(function i am using). Here's the code

#include <SFML/Graphics.hpp>

 int main(void)
 {

   sf::RenderWindow window(sf::VideoMode(640,480),"SFML working");

     return 0;
  }

And here's the error log.

cd '/home/jasper/NetBeansProjects/SFML'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/home/jasper/NetBeansProjects/SFML'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/sfml
make[2]: Entering directory '/home/jasper/NetBeansProjects/SFML'
mkdir -p dist/Debug/GNU-Linux
g++     -o dist/Debug/GNU-Linux/sfml build/Debug/GNU-Linux/main.o 
build/Debug/GNU-Linux/main.o: In function `main':

/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::String::String(char const*, std::locale const&)'

/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'

/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'

/home/jasper/NetBeansProjects/SFML/main.cpp:6: undefined reference to `sf::RenderWindow::~RenderWindow()'

collect2: error: ld returned 1 exit status
nbproject/Makefile-Debug.mk:62: recipe for target 'dist/Debug/GNU-Linux/sfml' failed
make[2]: *** [dist/Debug/GNU-Linux/sfml] Error 1
make[2]: Leaving directory '/home/jasper/NetBeansProjects/SFML'
nbproject/Makefile-Debug.mk:59: recipe for target '.build-conf' failed
make[1]: *** [.build-conf] Error 2
make[1]: Leaving directory '/home/jasper/NetBeansProjects/SFML'
nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failed
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 172ms)

I've tried searching for solution on google but couldn't find any effective one. So i thought some expert opinion would be good. Help please. Thanks

Sawaira Khan
  • 37
  • 1
  • 1
  • 4
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – arrowd Sep 30 '17 at 07:31
  • Note: that `void` in your `main` declaration is a C-ism and completely pointless (but harmless) in C++ - just remove it and have `int main()`. – Jesper Juhl Sep 30 '17 at 10:34

1 Answers1

6

You forgot to link with SFML libraries. Those are at least:

  • sfml-graphics
  • sfml-window
  • sfml-system

As the documentation explains, on Linux it can be done like this:

g++ myapp.o -o myapp -lsfml-graphics -lsfml-window -lsfml-system

Or if using an IDE, specify them as Additional Dependencies in Linker settings.

rustyx
  • 80,671
  • 25
  • 200
  • 267