undefined reference to

Apr 24, 2020 at 11:23am
I made a small program for testing the compilation with SFML library:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <SFML/Graphics.hpp>

int main()
{
    
    sf::RenderWindow window( sf::VideoMode(100,100), "Test" );
    
    while( window.isOpen() )
    {
        sf::Event event;
        while( window.pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
                window.close();
        }
        window.clear();
        window.display();
        sf::sleep( sf::milliseconds(30) );
    }
}


But I get this linker error:
1
2
3
4
c++ -o test test.cpp -lsfml-graphics -lsfml-window -lsfml-system
/tmp/ccMK05vT.o: In function `main':
test.cpp:(.text+0xf3): undefined reference to `sf::WindowBase::isOpen() const'
test.cpp:(.text+0x114): undefined reference to `sf::WindowBase::pollEvent(sf::Event&)' 

Apr 24, 2020 at 1:05pm
As you have rightly commented, an undefined reference to error is generated by a linker during the linking phase of the build process. It occurs when a declaration for a symbol can be resolved during compilation (satisfying the compiler's requirements), but the linker can't find the definition of the symbol during the linking process.

The declarations of the sf::WindowBase::isOpen() const and sf::WindowBase::pollEvent(sf::Event&) symbols are within the SFML/Graphics.hpp header file. However, the definition or implementation of the symbol is contained within the SFML libraries.

Are you sure the build log output you have posted is accurate and full? For example, where is your include directory flag? Please post the full and accurate output of your build command or script.

Linker errors typically occur due to the configuration of the system being used to build the software article in conjunction with the linker settings used for the specific software project.

As such, could you please let me know the following:

1. What platform are you building on?
2. What compiler collection are you using (including version)?
3. What version of SFML are you trying to use?
4. Did you build the SFML libraries yourself, or did you obtain pre-built binaries?
5. Where did you install SFML?
6. What is the full path to the root of your project directory?
Apr 24, 2020 at 1:38pm
What compiler/linker are you using? I see the command being used is c++.
Apr 24, 2020 at 1:52pm
Thanks for your help, could fix the problem.

I accidentally had a self-compiled SFML version installed, which I accidentally had overwritten with an older version of pre-compiled packages from the Ubuntu repository. I uninstalled the repository packages, and now it compiles fine.
Topic archived. No new replies allowed.