Knut Zuidema
Knut Zuidema

Reputation: 53

undefined reference when using SFML

I am trying to compile a program using the SFML library

My source looks like this:

//main.cpp
#include <SFML/Graphics.hpp>
int main(){
    sf::Color c;
}

I am compiling like this
g++ -IC:/dev/include -c main.cpp
and linking like this
g++ -LC:/dev/lib -lsfml-system -lsfml-window -lsfml-graphics -lsfml-audio -lsfml-network main.o -o main.exe

I am getting this error when linking:
main.o:main.cpp:(.text+0x1e): undefined reference to '__imp__ZN2sf5ColorC1Ev'

What am I doing wrong?
I compiled the SFML binaries myself using gcc 6.1 and cmake

Upvotes: 1

Views: 1959

Answers (1)

user7860670
user7860670

Reputation: 37468

Try writing your command like this, specifying libraries after input:

g++ -o main.exe main.o -LC:/dev/lib -lsfml-system -lsfml-window -lsfml-graphics -lsfml-audio -lsfml-network

The order of linking parameters is important because linker will proceed inputs sequentially. For each item it will fill existing list of undefined symbols with symbols exported from the current item, and then populate that list with symbols not defined in current item. So when main.o is last list of undefined symbols contains everything required by main.o, but there is no more items (libraries) to fill those missing symbols. Note that this implies that the order of libraries is important as well so more "generic" libraries should be kept closer to the end of the list.

Upvotes: 1

Related Questions