1

I'm new to the SFML library. I'm trying to compile an example program.

I have got the following sample code from SFML official documentation:

#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

int main()
{
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
    // Load a sprite to display
    sf::Texture texture;
    if (!texture.loadFromFile("cute_image.jpg"))
        return EXIT_FAILURE;
    sf::Sprite sprite(texture);
    // Create a graphical text to display
    sf::Font font;
    if (!font.loadFromFile("arial.ttf"))
        return EXIT_FAILURE;
    sf::Text text("Hello SFML", font, 50);
    // Load a music to play
    sf::Music music;
    if (!music.openFromFile("nice_music.ogg"))
        return EXIT_FAILURE;
    // Play the music
    music.play();
    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                window.close();
        }
        // Clear screen
        window.clear();
        // Draw the sprite
        window.draw(sprite);
        // Draw the string
        window.draw(text);
        // Update the window
        window.display();
    }
    return EXIT_SUCCESS;
}

I tried to compile it with the following command:

g++.exe code.cpp -s -lsfml-main -lsfml-audio-s -lflac \
    -lvorbisenc -lvorbisfile -lvorbis -logg -lsfml-network-s \
    -lws2_32 -lsfml-graphics-s -lfreetype -lglew32 -ljpeg \
    -lsfml-window-s -lopengl32 -lgdi32 -lsfml-system-s -lwinmm \
    -lgdi32 -luser32 -lkernel32 -lcomctl32 -lmingw32 \
    -luser32 -lgdi32 -ldxguid -mwindows \
    -std=c++11  -o code.exe

But I got these linker errors (paths shortened):

../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x6de): undefined reference to `_imp__alSourcePlay'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x7c8): undefined reference to `_imp__alSourcePause'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xa66): undefined reference to `_imp__alGetSourcef'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xb7a): undefined reference to `_imp__alBufferData'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xb96): undefined reference to `_imp__alSourceQueueBuffers'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xc9d): undefined reference to `_imp__alBufferData'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xcb9): undefined reference to `_imp__alSourceQueueBuffers'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xdd8): undefined reference to `_imp__alGenBuffers'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xdfd): undefined reference to `_imp__alSourcePlay'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xed0): undefined reference to `_imp__alGetSourcei'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xf0b): undefined reference to `_imp__alSourceUnqueueBuffers'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0xfed): undefined reference to `_imp__alGetBufferi'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x100a): undefined reference to `_imp__alGetBufferi'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x1160): undefined reference to `_imp__alSourceStop'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x1180): undefined reference to `_imp__alGetSourcei'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x11b8): undefined reference to `_imp__alSourceUnqueueBuffers'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x11ef): undefined reference to `_imp__alSourcei'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x1204): undefined reference to `_imp__alDeleteBuffers'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x126d): undefined reference to `_imp__alSourcePause'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x12e7): undefined reference to `_imp__alGetSourcei'
../lib32/libsfml-audio-s.a(SoundStream.cpp.obj):SoundStream.cpp:(.text+0x12f1): undefined reference to `_imp__alSourceUnqueueBuffers'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0xe): undefined reference to `_imp__alcOpenDevice'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x2c): undefined reference to `_imp__alcCreateContext'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x42): undefined reference to `_imp__alcMakeContextCurrent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x9b): undefined reference to `_imp__alListenerf'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0xc6): undefined reference to `_imp__alListener3f'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0xdb): undefined reference to `_imp__alListenerfv'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x19c): undefined reference to `_imp__alcMakeContextCurrent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x1ae): undefined reference to `_imp__alcDestroyContext'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x1c0): undefined reference to `_imp__alcCloseDevice'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x1f9): undefined reference to `_imp__alListenerf'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x24e): undefined reference to `_imp__alListener3f'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x2e4): undefined reference to `_imp__alListenerfv'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x384): undefined reference to `_imp__alListenerfv'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x445): undefined reference to `_imp__alGetEnumValue'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x47d): undefined reference to `_imp__alcMakeContextCurrent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x48f): undefined reference to `_imp__alcDestroyContext'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x4a9): undefined reference to `_imp__alcCloseDevice'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x501): undefined reference to `_imp__alGetEnumValue'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x521): undefined reference to `_imp__alGetEnumValue'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x541): undefined reference to `_imp__alGetEnumValue'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x59e): undefined reference to `_imp__alcMakeContextCurrent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x5b8): undefined reference to `_imp__alcDestroyContext'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x5d2): undefined reference to `_imp__alcCloseDevice'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x6be): undefined reference to `_imp__alIsExtensionPresent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x6e2): undefined reference to `_imp__alcMakeContextCurrent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x6f4): undefined reference to `_imp__alcDestroyContext'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x70d): undefined reference to `_imp__alcCloseDevice'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x7ae): undefined reference to `_imp__alcIsExtensionPresent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x807): undefined reference to `_imp__alcMakeContextCurrent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x820): undefined reference to `_imp__alcDestroyContext'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text+0x839): undefined reference to `_imp__alcCloseDevice'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text$_ZNSt8auto_ptrIN2sf4priv11AudioDeviceEED1Ev[__ZNSt8auto_ptrIN2sf4priv11AudioDeviceEED1Ev]+0x13): undefined reference to `_imp__alcMakeContextCurrent'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text$_ZNSt8auto_ptrIN2sf4priv11AudioDeviceEED1Ev[__ZNSt8auto_ptrIN2sf4priv11AudioDeviceEED1Ev]+0x25): undefined reference to `_imp__alcDestroyContext'
../lib32/libsfml-audio-s.a(AudioDevice.cpp.obj):AudioDevice.cpp:(.text$_ZNSt8auto_ptrIN2sf4priv11AudioDeviceEED1Ev[__ZNSt8auto_ptrIN2sf4priv11AudioDeviceEED1Ev]+0x37): undefined reference to `_imp__alcCloseDevice'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x62): undefined reference to `_imp__alSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x7a): undefined reference to `_imp__alDeleteSources'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x132): undefined reference to `_imp__alSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x14a): undefined reference to `_imp__alDeleteSources'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x21b): undefined reference to `_imp__alGenSources'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x23b): undefined reference to `_imp__alSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x2f1): undefined reference to `_imp__alGenSources'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x30b): undefined reference to `_imp__alSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x330): undefined reference to `_imp__alGetSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x356): undefined reference to `_imp__alSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x412): undefined reference to `_imp__alGetSource3f'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x44b): undefined reference to `_imp__alSource3f'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x46e): undefined reference to `_imp__alGetSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x58b): undefined reference to `_imp__alSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x5c1): undefined reference to `_imp__alSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x5fb): undefined reference to `_imp__alSource3f'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x63b): undefined reference to `_imp__alSource3f'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x66c): undefined reference to `_imp__alSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x69b): undefined reference to `_imp__alSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x6cb): undefined reference to `_imp__alSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x6fb): undefined reference to `_imp__alGetSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x72b): undefined reference to `_imp__alGetSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x776): undefined reference to `_imp__alGetSource3f'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x7ab): undefined reference to `_imp__alGetSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x7db): undefined reference to `_imp__alGetSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x80b): undefined reference to `_imp__alGetSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x82b): undefined reference to `_imp__alGetSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x84f): undefined reference to `_imp__alSourcef'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x8da): undefined reference to `_imp__alGetSource3f'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x906): undefined reference to `_imp__alSource3f'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x91e): undefined reference to `_imp__alGetSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x941): undefined reference to `_imp__alSourcei'
../lib32/libsfml-audio-s.a(SoundSource.cpp.obj):SoundSource.cpp:(.text+0x9db): undefined reference to `_imp__alGetSourcei'
collect2.exe: error: ld returned 1 exit status

I think it's due to improper order of static libraries in the compile command. What is the correct order for linking static SFML libraries?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • _"I think its due to improper order of static libraries in the compile command."_ Are you sure there **are** any static libraries in your command? Those look like dynamic ones to me. In which case, they need to be specified after the source file that depends on them; see [Why does the order in which libraries are linked sometimes cause errors in GCC?](https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc) – underscore_d Dec 14 '17 at 15:03
  • You didn't [read the doc](https://www.sfml-dev.org/faq.php#build-link-static), did you ? – Chnossos Dec 14 '17 at 15:03
  • yes, i read the docs but didn't get it useful – Abdullah Al Mahmud Dec 14 '17 at 15:09

1 Answers1

3

Your library order is fine (just having a quick look at it). You just forgot to link OpenAL (under Windows this would be -lOpenAL32), which is something to notice since all the undefined references start with _imp__al. Once done, everything should link and run as expected.

Mario
  • 35,726
  • 5
  • 62
  • 78